How to Check for Undefined in JavaScript
To compare values in JavaScript, the triple equal sign is the one you’re probably familiar with. The triple equals operator checks for strict equality between two values. Both the data type and value you’re comparing have to be precisely the same.
The double equals tests for loose equality and preforms type coercion. This means we compare two values after converting them to a common type.
Example of strict equality
// app.js console.log(11 === 11); console.log('Millie' === 'Millie'); console.log(true === true);
Output
true true true
Example of not strict equality
// app.js console.log(11 === '11'); console.log(undefined === 'undefined'); console.log(true === 'true');
Output
false false false
We can check JavaScript undefined check using a triple equals operator, and it returns the correct result.
JavaScript check undefined
To check undefined in JavaScript, use (===) triple equals operator. In modern browsers, you can compare the variable directly to undefined using the triple equals operator.
There are also two more ways to check if the variable is undefined or not in JavaScript, but those ways are not recommended. Those two are the following.
- JavaScript typeof operator
- JavaScript void operator
The above operators can check the variable against string ‘undefined’ and not undefined property.
So, it will raise an error in the production of the code in the future. So I recommend you don’t use those operators. Instead, use the triple equals to operator(==). It will give you the exact required result.
Syntax
if (name === undefined) {...}
Example
// app.js let data = undefined; if (data === undefined) { console.log('Yes in fact it is undefined'); }
Output
Yes in fact it is undefined
You can see that if statement holds true and it logged the statement.
From ECMAScript 5, The value of undefined is undefined. The undefined property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.
In modern browsers, it’s not possible to reassign the global undefined value to any variable.
However, for old browsers, javascript allowed undefined value to be re-assigned like this:
undefined = "data"
But that is not the case anymore.
JavaScript typeof
Javascript typeof is an inbuilt operator that returns the string, indicating a type of the unevaluated operand. The operator returns a data type of its operand in the form of the string.
// app.js let data = undefined; console.log(typeof (data));
Output
undefined
The typeof returns the string that tells the type of the operand. It is used without the parentheses, passing it any value you need to check. See the following code.
// app.js let data = undefined; if (typeof (data) === undefined) { console.log('Yes in fact it is undefined'); }
If you run the above code, then you won’t see the logged statement because the if statement returns false.
It is because of the typeof() operator checks and compares variables as a string and not other values. So, if you write like ‘undefined’, then it will return true and log the statement.
// app.js let data = undefined; if (typeof (data) === 'undefined') { console.log('Yes in fact it is undefined'); }
Output
Yes in fact it is undefined
We have written undefined as ‘undefined’, and now it matches with data variable and returns true.
If the value is not defined, the typeof returns an ‘undefined’ string.
That is why you don’t use the typeof() method to check if the variable is undefined or not in JavaScript.
JavaScript void Operator
The void operator checks the given expression and then returns undefined.
You can use a void operator to get the value of undefined. This will work even if the global window.undefined value has been overwritten.
See the following code and how to use the void operator to check for undefined.
// app.js let data = undefined; if (data === void(0)) { console.log('Yes in fact it is undefined'); }
Output
Yes in fact it is undefined
The zero(0) in the above code example doesn’t have any special meaning, and you could use 1 or function(){}. void(anything) will always evaluate to undefined.
Do this when using void or typeof
Avoid using void(0) or typeof data === “undefined” verbatim in code. These expressions aren’t self-explanatory and should be wrapped in user-defined function like isUndefined() function like below.
// app.js let data = undefined const isUndefined = (value) => { let undefined = void (0); return value === undefined; } console.log(isUndefined(data));
Output
true
From the output, you can see that the data is undefined, and the function returns true. If you want to use void(), then this is how you can use it, but again I am not recommending this solution.
Conclusion
To check if any variable is undefined in JavaScript, use triple equals operator (===). The triple equality operator has full support for modern browsers, and it returns the correct result.