Detecting an undefined object property

Solution:

You can use the following method to determine if a property’s value is the  undefined, special value:

if(o.myProperty === undefined) {
  alert("myProperty value is the special value `undefined`");
}

You can check to see if the object has such a property. If it does, it will return  undefinedby default.

if(!o.hasOwnProperty('myProperty')) {
  alert("myProperty does not exist");
}

To verify that the value associated to an identifier’s value is the special value undefined, Or If the identifier is not declared, This is the only way to refer to an undeclared Note: This is different than having a value  undefined). Identifier without an early error

if(typeof myVariable === 'undefined') {
  alert('myVariable is either the special value `undefined`, or it has not been declared');
}

JavaScript versions before ECMAScript 5 had the property “undefined” as a global object. This property was writable. A simple check foo === undefined  could behave unexpectedly if the property was accidentally redefined. Modern JavaScript makes the property read-only.

Modern JavaScript does not recognize “undefined” as a keyword. Therefore, variables within functions can be called “undefined” to shadow the global property.

You can use the void operator to find the  undefined  value if you are concerned about this edge case.

if(myVariable === void 0) {
  alert("myVariable is the special value `undefined`");
}

This topic has many wrong answers, according to me. Contrary to popular belief, “undefined” is not a term. Not A JavaScript keyword can have a value.

Correct code

This is the most reliable way to do this test:

if (typeof myVar === "undefined")

This will always return the correct result and can even handle situations where  myVar  has not been declared.

Degenerate code. Do not use.

var undefined = false;  // Shockingly, this is completely legal!
if (myVar === undefined) {
    alert("You have been misled. Run away!");
}

In the event that myVar is not declared,  myVar === undefined  will also raise an error

typeof despite being strongly recommended by many others here, is not a good choice. Typeof should not be used to check whether variables have undefined, values. It acts as both a check for  undefined, value and whether the variable exists. You will know in most cases when a variable is present. typeof can only be used to check for silent failures if there are typos in the variable name and string literal undefined.

var snapshot = …;

if (typeof snaposhot === 'undefined') {
    //         ^
    // misspelled¹ – this will never run, but it won’t throw an error!
}
var foo = …;

if (typeof foo === 'undefned') {
    //                   ^
    // misspelled – this will never run, but it won’t throw an error!
}

If you are doing feature detection 2 and there is uncertainty about whether a given name will fall within the scope of the code (such as checking  typeof module !== 'undefined'  in CommonJS environments),  typeof  can be a dangerous choice when used with a variable. It is better to directly compare the value.

var foo = …;

if (foo === undefined) {
    ⋮
}

Some common misconceptions include:

  • Reading an “uninitialized variable”  (var foo), or parameter  (function bar(foo) { … }, also known as bar()) , will result in an error. This is false. Variables without explicit initialization or parameters that were not given values are  undefined,  and always within their scope.
  • It is possible to overwrite the undefined . Although  undefined is not a keyword, it can be overwritten. Other built-ins are not to be avoided despite their non-keyword status. If you’re writing code generators, you can use  void 0.)

Now that we have an understanding of how variables work, let’s get to the real question: what are object properties?  typeof is not a good choice for object properties. This exception to feature detection is not applicable.  typeof has special behavior only on variables. Expressions that refer to object properties are not variables.

This:

if (typeof foo.bar === 'undefined') {
    ⋮
}

This3 is always the exact same as this3.

if (foo.bar === undefined) {
    ⋮
}

Take into consideration the above advice to avoid confusing readers about why you’re using  typeof . It makes the most sense for  === to check equality. This is because it can be refactored later to check a variable’s value and it just looks better.You should use === undefined³ here too.

Another thing to think about when you are looking at object properties is whether or not you want to verify that undefined  exists. An object can have a given property name that is absent (producing undefined  value when read), present on the object with undefined  value, present on its prototype with undefined  value, or present on any of these objects with undefined  value. 'key' in obj will tell you whether a key is anywhere on an object’s prototype chain, and Object.prototype.hasOwnProperty.call(obj, 'key')  will tell you whether it’s directly on the object. This answer will not cover prototypes and string-keyed mapping objects. It is intended to counter the bad advice contained in the other answers, regardless of how they are interpreted. For more information, visit MDN’s object prototypes page.

One unusual choice for an example variable name This is the No Script extension for Firefox.

2 do not assume that knowing the scope of an item is OK in general. Bonus vulnerability due to abuse of dynamic scope Project Zero 1225

3. Assuming an ES5+ environment again, where  undefined  refers the  undefined  property the global object.

Exit mobile version