Is there any differences between
var a;
(a == undefined)
(a === undefined)
((typeof a) == "undefined")
((typeof a) === "undefined")
Which one should we use?
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.
Ironically,
undefinedcan be redefined in JavaScript, not that anyone in their right mind would do that, for example:at which point all future equality checks against
undefinedwill yeild unexpected results!As for the difference between
==and===(the equality operators), == will attempt to coerce values from one type to another, in English that means that0 == "0"will evaluate to true even though the types differ (Number vs String) – developers tend to avoid this type of loose equality as it can lead to difficult to debug errors in your code.As a result it’s safest to use:
When checking for undefinedness 🙂