1. Introduction
In this tutorial, we’ll discuss undefined
in JavaScript. This concept is little bit different from other popular languages like Java. In Java null
is used to show absence of value. In JavaScript, people are confused about null
and undefined
as both look like absence of value. In this tutorial, we’ll try to explain undefined
and how it is used.
undefined
is a primitive value in JavaScript. This value is automatically assigned to variables which are declared but not assigned.
For example:
var x; console.log(x); // undefined
undefined
is also assigned to arguments in a function for which there are no actual arguments passed.
function test(x) { console.log("value of x: " + x); // value of x: undefined } test();
1. A variable which is not assigned a value has initial value of
undefined
.2. A function which does not return a value explicitly, returns a value of
undefined
.
function test() {} console.log("value returned by test: " + test()); // value returned by test: undefined
2. undefined
as a property
undefined
is a non-writable, non-configurable, non-enumerable property of global object. This property represents the primitive value undefined
.
In JavaScript, there’s always a global object defined, depending on the execution context. Window
is the global object in browser. Node.js has global
as the global object. The initial value of this undefined
property of global object is undefined
. Even if this is the initial value, you should not try to override it.
3. typeof
with undefined
typeof
operator returns ‘undefined’ when used for undefined
.
console.log(typeof undefined); // 'undefined'
So, typeof
can be used in comparison as well.
var x; if (typeof x === "undefined") { console.log("x is undefined"); // will be executed. }
4. Comparison of undefined
4.1 Comparison using strict equality operator
You should use strict equality operator (===) for comparison with undefined
.
var x; if (x === undefined) { console.log("x is undefined"); // will be executed } else { console.log("x is not undefined"); // will not be executed }
The reason for not using standard equality operator is, it checks only for values and not for type. Since both null
and undefined
mean absence of value, it returns true
.
console.log(null == undefined); // true console.log(null === undefined); // false
null
is not equivalent toundefined
.
4.2 Comparison using typeof
As mentioned earlier, typeof
can be used for comparison.
var x; if (typeof x === "undefined") { console.log("x is undefined"); // will be executed. }
4.3 Comparison of undefined using void
void 0
is often used to get undefined
. We can use the same in comparison as well. Previous comparison code with void can be written as:
var x; if (x === void 0) { console.log("x is undefined"); // will be executed. }
5. Important points
undefined
is a primitive value.undefined
is a property in global object which represents primitive valueundefined
.undefined
is not a reserved word. So you can define a variable namedundefined
. But you should not do it.- A variable which is not assigned a value and has only declared has default value as
undefined
. - A function which does not return a value returns
undefined
. undefined
property in global object is non-writable, non-configurable, non-enumerable.typeof
operator when used with undefined returns ‘undefined’.null
is not equivalent toundefined
.
6. Conclusion
In this tutorial, we discussed undefined
and how it is used in comparison. Understanding the use of undefined
is very important as it is regularly used in comparison. Similar to check for null
, check for undefined
in equally important. Knowledge of undefined
and it is not equivalent to null
is important from interview point of view as well. These are one-line tricky questions asked during the interview.
We hope this tutorial was helpful.