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.
the same applies strings "1" * 1 equals 1 because javascript automatically converts "1" number because of presence of multiplication operator (*).
Comments
Post a Comment