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:
- Convert an Object to JSON String using JavaScript
- Add a key/value pair to an object in javascript
- Cannot convert undefined or null to Object in JavaScript

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python