2

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
SOUser
  • 610
  • 1
  • 11
  • 25
  • One more: http://stackoverflow.com/questions/8330499/operator-before-expression-in-javascript-what-does-it-do – bfavaretto May 17 '13 at 20:27

1 Answers1

10

+ 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() )
jAndy
  • 231,737
  • 57
  • 305
  • 359