0

In JavaScript, it seems you can do either write:

new Date().getTime();

...or:

(new Date).getTime();

The first one is logical, but the second one seems a little unusual to me... Is there any difference between these two ways of creating a Date object, and what is the purpose of the second?

Thanks,

Steve

1 Answer 1

4

It seems that in javascript you can call constructor without parethesis. At least it works with my Firefox. So (new Date) == new Date()

Implying from that both expressions are equivalent. Alternatively you could write

(new Date()).getTime(); 

Which is what I usually do.

And I think it's just matter of personal preference. The new operator takes precedence before the . operator but the visual might suggest the other way round...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.