Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript + operator [duplicate]

Tags:

javascript

What is the application of the plus operator in these cases? I have seen it used in these ways but don't see how it operates.

start = +new Date;

+array[i]

+f.call(array, array[i], i)

x = +y
like image 286
SOUser Avatar asked Apr 15 '26 03:04

SOUser


1 Answers

+ will implicitly cast a string / boolean value into a Number().

+"66" === 66

If the string cannot be converted into a Number, the value will be NaN

+"not possible" // evaluates to NaN

In the case of a Date() object, + will also cast the data into its numerical representation, that is the UNIX timestamp.

So, finally spoken, leading an expression with + is pretty much the same as explicitly wrapping the Number() constructor around it:

+new Date()

equals

Number( new Date() )
like image 175
jAndy Avatar answered Apr 17 '26 17:04

jAndy



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!