The output of console.log(anObject) is misleading; the state of the object displayed is only resolved when you expand the Object tree displayed in the console, by clicking on >. It is not the state of the object when you console.log'd the object.

Instead, try console.log(Object.keys(config)), or even console.log(JSON.stringify(config)) and you will see the keys, or the state of the object at the time you called console.log.

You will (usually) find the keys are being added after your console.log call.

Answer from Matt on Stack Overflow
🌐
Atomizedobjects
atomizedobjects.com › blog › javascript › how-to-check-if-an-object-property-exists-but-is-undefined-in-javascript
How to check if an object property exists but is undefined in JavaScript | Atomized Objects
In this post we are going to look at how you can check if a property exists in JavaScript. The easiest way to check if an object property exists but is undefined in JavaScript is to make use of the Object.prototype.hasOwnProperty method which will let you find properties in an object.
Top answer
1 of 16
2957

The usual way to check if the value of a property is the special value undefined, is:

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

To check if an object does not actually have such a property, and will therefore return undefined by default when you try to access it:

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

To check if the value associated with an identifier is the special value undefined, or if that identifier has not been declared:

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

Note: this last method is the only way to refer to an undeclared identifier without an early error, which is different from having a value of undefined.

In versions of JavaScript prior to ECMAScript 5, the property named "undefined" on the global object was writeable, and therefore a simple check foo === undefined might behave unexpectedly if it had accidentally been redefined. In modern JavaScript, the property is read-only.

However, in modern JavaScript, "undefined" is not a keyword, and so variables inside functions can be named "undefined" and shadow the global property.

If you are worried about this (unlikely) edge case, you can use the void operator to get at the special undefined value itself:

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

I believe there are a number of incorrect answers to this topic. Contrary to common belief, "undefined" is not a keyword in JavaScript and can in fact have a value assigned to it.

Correct Code

The most robust way to perform this test is:

if (typeof myVar === "undefined")

This will always return the correct result, and even handles the situation where myVar is not declared.

Degenerate code. DO NOT USE.

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

Additionally, myVar === undefined will raise an error in the situation where myVar is undeclared.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › undefined
undefined - JavaScript | MDN
// x has not been declared before // evaluates to true without errors if (typeof x === "undefined") { // these statements execute } // Throws a ReferenceError if (x === undefined) { } However, there is another alternative. JavaScript is a statically scoped language, so knowing if a variable is declared can be read by seeing whether it is declared in an enclosing context. The global scope is bound to the global object, so checking the existence of a variable in the global context can be done by checking the existence of a property on the global object, using the in operator, for instance:
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Object is undefined, though I define it - JavaScript - The freeCodeCamp Forum
June 2, 2021 - I am befuddled as to why I’m receiving the undefined value pertaining to variables and objects below. let nameObj = { last: "Capulet" }; console.log(Object.keys(nameObj)) // prints 'last' let key = Object.keys(nameObj)…
🌐
The Valley of Code
thevalleyofcode.com › how-to-check-undefined-property-javascript
How to check if a JavaScript object property is undefined
In a JavaScript program, the correct way to check if an object property is undefined is to use the typeof operator.
🌐
Reddit
reddit.com › r/webdev › property of element returns undefined but the value exists
r/webdev on Reddit: Property of element returns undefined but the value exists
December 23, 2022 - You can't set object (or array) properties using a JSON string in the element attribute like this. You need to be setting it from your JavaScript on the element instance (the return from getElementById).
🌐
Dmitri Pavlutin
dmitripavlutin.com › check-if-object-has-property-javascript
3 Ways to Check If an Object Has a Property/Key in JavaScript
January 25, 2023 - The 3 ways to check if an object has a property or key in JavaScript: hasOwnProperty() method, in operator, comparing with undefined.
Find elsewhere
🌐
Byby
byby.dev › js-check-undefined
Check if object property is undefined in JavaScript
Using Lodash _.isNil() function to check if object property is null or undefined. var _ = require("lodash"); var obj = { name: "Charlie", age: 30, hobbies: ["cooking", "biking", "gardening"], address: null, score: 0, email: undefined }; for (var prop in obj) { if (_.isNil(obj[prop])) { ...
🌐
Medium
lakin-mohapatra.medium.com › how-to-handle-undefined-properties-in-an-object-in-javascript-46bcdcfdedbc
How to handle undefined properties in an object using javascript? | by Lakin Mohapatra | Medium
December 22, 2022 - const obj = { a: 1 }; console.log(obj.b?.c); // Outputs "undefined" 2. Use the in operator: This operator checks if an object has a particular property, and then allows you to access the property only if it exists.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-detect-an-undefined-object-property-in-javascript
How to Detect an Undefined Object Property in JavaScript ? - ...
July 23, 2025 - Approaches 3: Using the ... (as opposed to inheriting it from its prototype chain). If the property is undefined, hasOwnProperty will return false....
🌐
Codecademy
codecademy.com › forum_questions › 54217f8e52f8633fc1002eb9
Why are my objects are undefined? | Codecademy
EDIT: What I want is, to show all the data of my objects through this function..but its not working Another thing: How to use “.this”? I saw in an example that they used it to show all values in each key of object..but maybe I am not performing it right? var object1 = { list: 'one', list2: 'two' }; var object2 = new Object(); object2.list3 = 'three'; object2.list4 = 'four'; var object3 = new Object(); object3['list5'] = 'five'; object3['list6'] = 'six'; var calling = function () { for (i = 1; i<=6; i++) { console.log(this.list+[i]); }; }; calling(); ... Each Object has one or more properties.
Top answer
1 of 2
14

I've solved the problem. Basically, the object in question (that.data[0].cards) has its properties created by a function a() that runs after all the AJAX requests for the necessary XML files have been processed. I allow the requests to run asynchronously, using a counter to determine in the success callback function if a() should be called yet.

After a() runs, function b() is supposed to perform operations on that.data[i].cards. However, b() was running prior to a() being called because of a()'s reliance on the asynchronous requests. So the solution was simply to make a() call b().

So this turned out to be a pretty simple mistake on my part. What made it so confusing was the fact that logging that.data[0].cards to the console showed me that in fact the cards object had already been built, when in fact it had not yet. So the console was providing me with incorrect--or at least unclear--information.

Thanks for everyone's help last night! Upvotes all around :)

2 of 2
8

I think the object keys have unprintable characters, such can be replicated like this:

var obj = {};
obj["E"+String.fromCharCode(15)] = new Array(15);

console.log(obj);

/*Object
E: Array[15]
__proto__: Object*/

console.log(obj.E)

//undefined

console.log( obj["E"+String.fromCharCode(15)] )

//[]

Edit: you can see if this is the case for your object keys:

var realKeys = [];

for( var key in obj ) {
realKeys.push( [].slice.call( key ).map( function(v){return v.charCodeAt(0);} ).join(" ") );
}

//["69 15"] (69 stands for the letter "E" and 15 was the unprintable character I added manually)

Edit2: Since you can't do that I came up with another way to see if there are unprintable characters:

Copypaste the key string like this: (go all the way as much as you can on both ends so you pick any invisible characters)

Then dump your clipboard like this (Make sure you are using double quotes):

🌐
Temp Mail
tempmail.us.com › temp mail › blog › javascript › javascript checking for undefined object properties
JavaScript Checking for Undefined Object Properties
July 24, 2024 - In addition to the previously stated approaches, another handy strategy to detect undefined object properties is the usage of the Object.keys method. This function returns an array of a given object's enumerable property names. You can find out if a property exists by checking whether it is in this array. This method is very useful when you need to examine numerous properties at once or when dealing with dynamically produced objects. Additionally, JavaScript's try...catch statement can be used to manage problems that may occur while accessing properties of undefined objects.
🌐
Medium
medium.com › @python-javascript-php-html-css › checking-for-undefined-object-properties-in-javascript-d62ea01c8ba6
JavaScript Checking for Undefined Object Properties
August 24, 2024 - The most reliable method is using the hasOwnProperty method, as it checks for the property directly on the object without traversing the prototype chain. Can I use the in operator to check for undefined properties?
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › check-if-property-is-undefined
How to Check if a Property is Undefined in JavaScript - Mastering JS
if (obj[key] === undefined) { // ... } The potential problem with this approach approach is that if obj doesn't have the property, it will also return true. To check if the object has the property, you can use in operator or hasOwnProperty() ...
🌐
Meteor
forums.meteor.com › help
Cannot read property of undefined, but property exists - help - Meteor.js forums
June 3, 2015 - Hello all, I am getting a curious error in a template helper and I was hoping someone could lay eyes on it with me. Basically the error I’m getting in the console of the client is that the getArena().height is undefined. However, console.log(getArena().height) returns the correct property value.