Subtle differences sometimes indicate the proficiency of a programmer.
var t1 = (new Date()).getTime(); // 1 var t2 = new Date().getTime(); // 2 var t3 = (new Date).getTime(); // 3
new Date creates a new
new Date.getTime() because the interpreter sees that as trying to call the constructor for a
Date.getTime class (cf.
MyPackage.MyClass) - in this case the brackets are required for the statement to parse as intended.
Of course, if you want the most compact code possible, you’d write:
var t4 = +new Date; // 4
This little gem creates a new Date, then coerces it into a number using a unary
+ operator - and coercing a Date to a number is defined in the language spec to go via
Checking the character-count:
(new Date).getTime() is 20 characters, while
+new Date is 9.
That 11 character saving might come in handy on, say, the new Google home-page, where they currently use
(new Date).getTime() seven times.
Update: the first three versions are all reformatted to
new Date().getTime() by prettier