13

How to Check If an Object Property is Undefined in JavaScript

 2 years ago
source link: https://www.laravelcode.com/post/how-to-check-if-an-object-property-is-undefined-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Check If an Object Property is Undefined in JavaScript

  972 views

  1 year ago

Javascript

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>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK