Q:

How to Check If an Object Property is Undefined in JavaScript

0

How to Check If an Object Property is Undefined in JavaScript

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer: Use the strict equality operator (===)

You can use the typeof operator in combination with the strict equality operator (===) to check or detect if a JavaScript object property is undefined.

Let's take a look at the following example to understand how it actually works:

<script>
var person = {
    name: "Clark",
    age: 32,
    country: "United States"
};

// Access an existing object property
console.log(person.age); // Prints: 32

// Detect if object property is undefined
if(typeof person.gender === "undefined"){
    console.log("This property is not defined.");
}
</script>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Capture Browser Window Resize Event in Java... >>
<< How to Find the Max and Min Values of an Array in ...