How to check for "undefined" value in JavaScript ? Last Updated : 28 May, 2024 Comments Improve Suggest changes Like Article Like Report In JavaScript, undefined is a primitive value that represents the absence of a value or the uninitialized state of a variable. It's typically used to denote the absence of a meaningful value, such as when a variable has been declared but not assigned a value. It can also indicate the absence of a return value in a function or the absence of a defined property in an object. Understanding undefined is crucial for writing robust and error-free JavaScript code. There are a few ways to check for 'undefined'. Using the 'typeof' operator Comparing with the 'undefined' value1. Using the 'typeof' operator:Here, 'typeof' operator returns the string 'undefined' if the variable has not been assigned a value. JavaScript // Declare a variable let myVariable; // Condition to check variable is defined or not if (typeof myVariable === "undefined") { console.log("myVariable is undefined"); } else { console.log("myVariable is defined"); } OutputmyVariable is undefined Comparing with the 'undefined' valueHere, the '===' operator checks if the value of the variable is exactly equal to 'undefined'. JavaScript // Declare a variable let myVariable; // Condition to check variable is defined or not if (myVariable === undefined) { console.log("myVariable is undefined"); } else { console.log("myVariable is defined"); } OutputmyVariable is undefined Example 1: JavaScript // Using the 'typeof' operator: // Declare a variable let fruit; // Condition for check variable is defined or not if (typeof fruit === "undefined") { console.log("fruit is undefined"); } else { console.log("fruit is defined"); } Outputfruit is undefined Example 2: JavaScript // Using the 'typeof' operator: // Declare a variable and assign a value // it will return fruit is defined let fruit = "apple"; // Condition for check variable is defined or not if (typeof fruit === "undefined") { console.log("fruit is undefined"); } else { console.log("fruit is defined"); } Outputfruit is defined Example 3: JavaScript // Comparing with the 'undefined' value: // Declare a variable let profile; // Condition for check variable is defined or not if (profile === undefined) { console.log("profile is undefined"); } else { console.log("profile is defined"); } Outputprofile is undefined Example 4: JavaScript // Comparing with the 'undefined' value: // Declare a variable and assign value let profile = "geeksforgeeks"; // Condition for check variable is defined or not if (profile === undefined) { console.log("profile is undefined"); } else { console.log("profile is defined as", profile); } Outputprofile is defined as geeksforgeeks Comment More infoAdvertise with us Next Article How to check for "undefined" value in JavaScript ? S sunillaksh Follow Improve Article Tags : JavaScript Web Technologies Similar Reads How to check for null values in JavaScript ? The null values show the non-appearance of any object value. It is usually set on purpose to indicate that a variable has been declared but not yet assigned any value. This contrasts null from the similar primitive value undefined, which is an unintentional absence of any object value. That is becau 4 min read How to Check empty/undefined/null String in JavaScript? Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. Here are different approaches to check a string is empty or not.1. Using === OperatorUsing 2 min read How to handle an undefined key in JavaScript ? In this article, we will try to analyze how we may handle an undefined key (or a property of an object) in JavaScript using certain techniques or approaches (via some coding examples). Firstly let us quickly analyze how we may create an object with certain keys along with their values using the foll 3 min read How to Replace a value if null or undefined in JavaScript? In JavaScript, replacing a value if it is null or undefined involves providing a default value to use when the original value is either null or undefined. This ensures the application has valid data, preventing potential errors or unexpected behavior. Here we have some common approaches: Table of Co 2 min read How to check for undefined property in EJS for Node.js ? Handling undefined attributes is essential when working with EJS templates in a NodeJS application to guarantee a seamless and error-free user experience. In this post, we'll examine one of the most basic methods for determining whether a variable is defined or not, which involves utilizing a simple 3 min read Like