new Date() — JS Technical Interview Questions & Answers
Creates a Date object for the current or a given moment.
new Date()
- new Date() is a JS topic. In plain words you use it for a short new Date example in a script in the browser. Don’t start with a slogan — start with that picture.
- Smallest example: the smallest new Date example you can type. Type it, run it, and say what you see. If you can do that from memory, you know new Date().
- From the example next to this theory: Creates a Date object for the current or a given moment.
- Trap: only saying “new Date” with no example. Fix that before you talk about advanced JS.
- Viva: what is new Date()? Then show the smallest new Date example you can type. Then name the trap.
Technical Interview Tip: What is new Date()? Show this: the smallest new Date example you can type. Trap: only saying “new Date” with no example.
Code Examples for new Date()
new Date() — working example
// Topic: new Date()
const nums = [1, 2, 3];
const doubled = nums.map((n) => n * 2);
console.log(doubled);
console.log(doubled.reduce((a, b) => a + b, 0));new Date(): map then reduce on [1, 2, 3]. Say both prints.
How to Explain new Date() to an Interviewer
What is new Date()?
Used for a short new Date example a script in the browser Not just the heading
Give one small example of new Date().
the smallest new Date example you can type Creates a Date object for the current or a given moment
What mistake do beginners make with new Date()?
only saying “new Date” with no example Show the fix
Where do you use new Date()?
a script in the browser a short new Date example