Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between new Date().valueOf() and new Date() * 1 in javascript

Tags:

javascript

What is the difference between

new Date().valueOf() 

and

new Date() * 1

Both give the same value, is there any performance difference? (Just out of curiosity)

like image 623
Prasad K - Google Avatar asked Feb 10 '26 23:02

Prasad K - Google


1 Answers

Using an object in a multiplication expression implicitly involves a call to .valueOf() anyway, so there's really no difference at all. That is, the way that the expression

new Date() * 1

is interpreted involves an attempt to get the operand on the left side of the * operator to be a number. That's what the .valueOf() method is supposed to do. For Date instances, that returns the millisecond timestamp value.

Note that

Date.now()

is also equivalent. (Not new Date().now(); the "now" function is a property of the Date constructor.)

like image 108
Pointy Avatar answered Feb 13 '26 14:02

Pointy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!