Check if an Object Property is Undefined in JavaScript-Code Examples

Radu TM

Check if an Object Property is Undefined in JavaScript

JavaScript by Radu TM June 21, 2022

const obj = {
  name: 'John',
  age: undefined,
};

// name is defined
console.log(obj.name !== undefined); // true

// age is undefined
console.log(obj.age === undefined); // true

// does not exist
console.log(obj.doesNotExist === undefined); // true

0

20.814

Check if an Object Property is Undefined in JavaScript

JavaScript by Radu TM June 21, 2022

const obj = {
  name: 'John',
  age: undefined
};

// Check if "age" property is undefined
if (obj.age === undefined) {
  console.log('The "age" property is undefined');
} else {
  console.log('The "age" property is defined');
}

0

20.814

Check if an Object Property is Undefined in JavaScript

JavaScript by Radu TM June 21, 2022

if (typeof myObject.myProperty === "undefined") {
  // do something
}

0

20.814

Check if an Object Property is Undefined in JavaScript

JavaScript by Radu TM June 21, 2022

if (typeof myObject.myProperty === "undefined") {
  // do something
}

0

20.814

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories