Warning: session_start(): open(/tmp/sess_ad5613220b616af8cadffaffa8d51d1d, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check if an Object Property is Undefined in JavaScript - LearnShareIT

How To Check if an Object Property is Undefined in JavaScript

Check if an Object Property is Undefined in JavaScript

Suppose you don’t know how to check if an object property is undefined in JavaScript. Don’t worry, we will show you some solutions in this article.

Check if an Object Property is Undefined in JavaScript

Using the typeof operator

The typeof operator returns a string indicating the type of the operand’s value.

Syntax

typeof operand

Parameters

The operand is an expression representing the object or primitive whose type is to be returned.

Example 1

console.log(typeof 23); // Will show the output is "number."
console.log(typeof 'LearnShareIT'); // Will show the output is "string."
console.log(typeof true); // Will show the output is "boolean."
console.log(typeof color); // Will show the output is "undefined."

Output

number
string
boolean
undefined

Using the strict equality operator (===)

To check or detect if a JavaScript object property is undefined, we use the typeof‘ operator in combination with the strict equality operator (===). Let’s see the example below:

Example 2:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Detect an Undefined Object Property in JavaScript - Learn Share IT </title>
</head>
<body>
    <script>
        var supplier = {
            name: "LearnShareIT",
            address: "Washington DC",
            country: "The United States",
            Founded_year: 1999
        };
        
        // Access an existing object property
        document.write(supplier.name); // Prints: LearnShareIT
        document.write(supplier.Founded_year); // Prints: 1999
      
        // Detect if object property is undefined
        if (typeof supplier.origin === "undefined") {
            document.write("<p>This property origin is not defined.</p>");
        }
    </script>
</body>
</html>     

Output

LearnShareIT
1999
This property origin is not defined.

Example 3: 

Create the isUndefined method and test origin and Major is undefined

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Create the isUndefined method and test origin and Major is undefined - Learn Share IT </title>
</head>
<body>
    <script>
        var supplier = {
            name: "LearnShareIT",
            address: "Washington DC",
            country: "United States",
            Founded_year: 1999
        };
      
      	function isUndefined(supplier) {
            if (typeof supplier.origin === 'undefined') {
                document.write("<p>origin is  undefined.</p>");
            }

            if (typeof supplier.major === 'undefined') {
                document.write("<p>Major is  undefined.</p>");
            }
        }
      
     	isUndefined(supplier);
    </script>
</body>
</html> 

Output

origin is undefined.
Major is undefined.

Summary

In this tutorial, we have explained how to check if an object property is undefined in JavaScript by using the typeof operation. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for your reading.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *