In JavaScript, you can use the typeof operator to check if a variable is undefined. If a variable is not initialized, its value will also be undefined, so you can also use typeof to check if a variable has been declared but not assigned a value.
In JavaScript, you can check if a variable is undefined using the typeof operator:
if (typeof something != "undefined") {
// ...
}Alternatively, you can also use the === operator to directly compare the variable to undefined:
if (variableName === undefined) {
// variable is undefined
}
JavaScript checks if undefined
A simple example code check is if a variable is undefined using the typeof operator.
// global scope
var theFu; // theFu has been declared, but its value is undefined
typeof theFu; // "undefined"
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:
<!DOCTYPE html>
<html>
<body>
<script>
if (typeof myVar == 'undefined')
{
alert("Hello variable")
}
</script>
</body>
</html>
Output:

Source: stackoverflow.com
Do comment if you have any doubts or suggestions on this JS if the topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version