Single plus operator in javascript [duplicate]

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?

Answers:

Answer

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

if ( obj.length === Number(obj.length) ) {
    // ...
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.