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
  • 615
  • 1
  • 11
  • 31
jviotti
  • 15,113
  • 22
  • 82
  • 134
  • @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
  • 193,614
  • 53
  • 339
  • 276