Date without brackets in Javascript

Can you use the Date object without brackets? Yes, apparently - to a friend’s delight and my chagrin. I should know better than to rely on my somewhat shaky Javascript knowledge.

This is the format for a sane Date code..

const today = new Date();
console.log(today);
// 2019-08-23T09:31:12.181Z

If you want to pass parameters -

const firstDay = new Date(2019, 0, 1);
console.log(firstDay);
// 2019-01-01T00:00:00.000Z

This simply means that firstDay is created by passing variables to Date, which are in turn being used by the date constructor.

If you do not have any variables for date, the following code is valid.

// prettier-ignore
const newToday = new Date;
console.log(newToday);
// 2019-01-02T00:00:00.000Z

The syntax without brackets is in fact valid for any object, not just date.

I was indeed surprised (and a bit hurt) that I never encountered this statement so far. The reason for that was far more logical - it turned out that Prettier does not like this syntax either and puts the brackets automatically.

Well, I did learn something that I may not possibly use for the rest of my life.

comments powered by Disqus