Undefined:- undefined variable is one that has declared but doesn’t have assigned a value. Undefined is not a keyword.
Undeclared:- It occurs when we don’t initialize a variable or declare it using var or const keywords. When we use typeof with undeclared variable it will throw an error with return value as ‘undefined’.
As here we need to check a variable for undefined:-
typeof is a built-in method which check whether a variable is undefined or defined.
typeof doesn’t throw any exception if we declare an undefined variable. We can also use typeof in cheking for null.
var s; console.log(typeof s); //print undefined var s1; console.log(s1===undefined); //print true //Since,here the variable s1 don’t have any value but the variable exists.//Here the assigned variables don’t have any value but the variable exisOutput:- var s3=56;; console.log(typeof s3); //print type of the value assigned: i.e, Number
Successfully checked if the variable is defined or undefined.