Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
111
rated 0 times [  114] [ 3]  / answers: 1 / hits: 31581  / 13 Years ago, mon, august 29, 2011, 12:00:00

Could somebody explain to me the difference between if(obj.x == undefined) and if(typeof obj.x == 'undefined')



In some context the first one works fine, but in other I need to use the second way.



Questions



1 - What is the difference between the two condition?



2 - Is there a best practice?


More From » javascript

 Answers
7

The best practice is to not just check the truthiness but the strict equality



example



if (obj.x === undefined) {}


this use to be an issue because undefined (a global property) use to be writable, as of 1.8.5 is is non-writable, providing you with a secure comparison in ES5 spec environments.



per MDN


[#90364] Friday, August 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
clarissakourtneyb

Total Points: 710
Total Questions: 89
Total Answers: 125

Location: Dominica
Member since Sat, Nov 5, 2022
2 Years ago
;