How do you distinguish a missing object property from one set to undefined?
Use the in operator or hasOwnProperty. 'a' in obj returns true if the property exists (even if its value is undefined). obj.a returns undefined whether the property is missing or set to undefined.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in How to Check for undefined in JavaScript
Use strict equality: x === undefined. This checks both type and value. It is the canonical, safe way to check if a declared variable has the value undefined.
Because !x also catches null, 0, '', false, and NaN. If you only want to check for undefined, use === undefined. Use !x only when you want to catch all falsy values.
Use typeof. typeof an undeclared variable returns 'undefined' without throwing. For example: if (typeof someGlobal !== 'undefined') is safe for feature detection.
Still have questions?
Browse all our FAQs or reach out to our support team
