How to determine if a variable is undefined
JavaScript undefined property indicates that a variable has not been assigned a value, or not declared at all. If you are interested in knowing whether the variable hasn't been declared or has the value undefined, then use the typeof operator, which is guaranteed to return a string, and doesn't generate an error if the variable doesn't exist at all.
let myVal;
if(typeof myVal !== "undefined") {
msg = "myVal is defined";
}
else{
msg= "myVal is undefined";
}
console.log(msg);//output: myVal is undefined
Or you can use:
let myVal;
if(typeof myVal === "undefined") {
msg = "myVal is undefined";
}
else{
msg= "myVal is defined";
}
console.log(msg); //output: myVal is undefined
(typeof myVal === 'undefined') Vs. (myVal === undefined)
The biggest difference between these two approaches is that, if myVal has not been declared, myVal === undefined throws a ReferenceError, but typeof does not. When using myVal === undefined, JavaScript checks if myVal is a declared variable that is strictly equal to undefined. If you want to check if myVal is strictly equal to undefined regardless of whether is has been declared or not, you should use typeof myVal === 'undefined'.
myVal === undefined; // Throws a ReferenceError
typeof myVal == 'undefined'; // true
However, typeof myVal may still throw an error if you later declare myVal using let or const:
typeof myVal; // Throws "ReferenceError: Cannot access 'myVal' before initialization" because of 'let'.
let myVal = 100;
JavaScript undefined

The undefined property indicates that a variable has not been assigned a value, or not declared at all. The undefined is a property of the global object. That is, it is a variable in global scope. It is a primitive type in JavaScript, so the undefined is a type. When you declare a variable and don't initialize it to a value, the variable will have a value of undefined.
let myVal;
console.log(typeof myVal); //output: undefined
Or
let myVal;
console.log(myVal); //output: undefined
How can you determine if a variable is 'undefined' or 'null'?
You can use the qualities of the abstract equality operator to do this:
if (myVal == null){
// do something here.
}
Because null == undefined is true, the above code will catch both null and undefined.
Related Topics
- JavaScript Popup Boxes
- Opening a new window in JavaScript
- How to Create Drop-Down Lists in JavaScript
- How do I include a JavaScript file in another JavaScript file?
- Print the content of a Div using JavaScript/jQuery
- How to get the current URL using JavaScript ?
- How to Detect a Mobile Device with JavaScript/jQuery
- How to validate an email address in JavaScript
- JavaScript Array Iteration
- How to Remove a Specific Item from an Array in JavaScript
- What is JavaScript closures?
- How To Remove a Property from a JavaScript Object
- How to get selected value from Dropdown list in JavaScript
- How do I get the current date in JavaScript?
- How to Open URL in New Tab | JavaScript
- How to delay/wait/Sleep in code execution | JavaScript
- How to round to at most 2 decimal places | JavaScript
- How to convert string to boolean | JavaScript
- How To Copy to Clipboard | JavaScript
- How to encode a URL using JavaScript?
- How to force Input field to enter numbers only | JavaScript
- How to create multiline string in JavaScript
- How to Check for an Empty String in JavaScript?