Why is underscore.js’s isUndefined defined this way?
_.isUndefined = function(obj) {
return obj === void 0;
};
Why can’t this work?
typeof obj === 'undefined'
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.
Ok, for a start
typeof obj === 'undefined'is slower as you can easily verify.The question then is why make the comparison
vs
Let’s see:
void 0;returns the result of the unary operatorvoidwhich will always returnundefined(i.e.void 1would make no difference)undefinedpoints to the global variableundefined.Under normal circumstances the two are the same. I presume though
void 0is preferred because it is possible to shadowundefinedwith a local variableundefined🙂 This is idiotic but it happens.