In underscore.js source in many places I came across
if (obj.length === +obj.length)
Can someone explain, why do they use it?
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.
It’s another way of writing
if (typeof obj.length == 'number'). Why they do it that way, it’s anyone’s guess. Probably trying to be clever at the expense of readability. Which is not too uncommon these days, unfortunately…Although it might be so that it can be compressed more by minifiers (YUI Compressor, Closure Compiler, UglifyJS, etc):
(a.length===+a.length)vs(typeof a.length=='number')Doing it their way would save 5 bytes, each instance.