I came across this line of code in Underscore.js’s _.each implementation and I am curious what is going on here. What does the ‘+’ in front of the obj do?
if (obj.length === +obj.length) { ... }
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
iftests thatobj.lengthis numeric and notNaN. The right-hand side is always a number (orNaNifobj.lengthcannot be interpreted as a number). It will only be===to the left-hand side ifobj.lengthis also a number.Note that using
isNaNwon’t work ifobj.lengthis a numeric-looking string; that is,isNan("3")returnsfalse. Note also thatNaN === NaNisfalse—NaNis never===to anything.