This page looks best with JavaScript enabled

Date without brackets in Javascript

 ·   ·  ☕ 1 min read

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..

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

If you want to pass parameters -

1
2
3
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.

1
2
3
4
// 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.

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things