In javascript whats the difference between
undefined == variable and variable == undefined are both of them same? and how different will it be if i do undefined === variable or typeof variable == 'undefined'?
Can someone help me out
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.
Don’t use
undefinedto test for an undefined variable, use the typeof operator instead!undefinedisn’t a javascript keyword, it’s just a variable name. If someone writesvar undefined = trueglobally anywhere into the code, all your comparisons will act unexpected.You should consider using something like JSLINT or JSHINT to avoid these type of errors in your javascript code.
Apart from that, I would always write the compared parameter first, as that’s the way I read it.
That is why
If the variable foo is undefined thanshould be written asif (typeof foo === "undefined")I don’t remember the name for this pattern, but I’m quite sure there is one 🙂