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


JavaScript: Check if Variable is undefined or null
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.