25

Just saw this in underscore's source:

if (obj.length === +obj.length) {
    ...
}

What does the plus do? I never saw this before.

Is it considered a good practice among developers?

jvdhooft
  • 657
  • 1
  • 12
  • 33
jviotti
  • 17,881
  • 26
  • 89
  • 148
  • @epascarello The point of this question is also if the named technique is considered a good practice among developers, which is not discused on the other question – jviotti Jan 23 '13 at 01:31
  • Good Practice is a "I think blue is best color" type of thing. One guy will say, no, use Number(), other guy will say use +. And the selected answer here does not say it. ;) – epascarello Jan 23 '13 at 02:09

1 Answers1

35

The plus converts a string to a float. The code you provided is equivalent to the following:

if ( obj.length === Number(obj.length) ) {
    // ...
}
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292