Why does 'new Date() * 1' produce a Javascript timestamp? -


i have following methods of obtaining timestamp:

new date().valueof() new date().gettime() date.parse(new date()) new date() * 1 

but i'm confused: why able timestamp using last method?

this because date() object can converted directly number (the timestamp), , when applying mathematical operators javascript converts us.

an quicker way timestamp use unary plus:

+new date(); 

the unary plus operator precedes operand , evaluates operand attempts converts number, if isn't already. although unary negation (-) can convert non-numbers, unary plus fastest , preferred way of converting number, because not perform other operations on number.

mdn's unary plus documentation.

the same applies strings "1" * 1 equals 1 because javascript automatically converts "1" number because of presence of multiplication operator (*).


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -