How to Check for undefined value
To check for undefined value , we can use the typeof to check if the value is undefined or not:
let myVariable;
if (typeof myVariable === "undefined") {
console.log("myVariable is undefined");
} else {
console.log("myVariable is defined");
}
Output
myVariable is undefined
Undefined in JavaScript
Undefined is a type of Data type in JavaScript. It is a primitive value undefined, when a variable is declared and not initialized or not assigned with any value. By default, the variable was stored with an Undefined value.
Undefined is a global read-only variable that represents the value Undefined. Undefined is a type of primitive type
Syntax:
You don’t explicitly create undefined
. It’s automatically assigned to variables that lack a value:
let variable = undefined;
// or
let x;
Both no and variable contain an undefined value.