Check If A Variable Is undefined or null in JavaScript
In this example, you will learn to write a JavaScript program that will check if a variable is undefined or null.
To understand this example, you should have the knowledge of the following JavaScript programming topics:
JavaScript null and undefined
JavaScript typeof Operator
JavaScript Function and Function Expressions
Example 1: Check undefined or null
In the above program, a variable is checked if it is equivalent to null.
The null with == checks for both null and undefined values. This is because null == undefined evaluates to true.
Example 2: using typeof
The typeof operator for undefined value returns undefined. Hence, you can check the undefined value using typeof operator. Also, null values are checked using the === operator.
Note: We cannot use the typeof operator for null as it returns object.
Post a Comment