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
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
+
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() )