Learnitweb

undefined in JavaScript

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 to undefined.

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

  1. undefined is a primitive value.
  2. undefined is a property in global object which represents primitive value undefined.
  3. undefined is not a reserved word. So you can define a variable named undefined. But you should not do it.
  4. A variable which is not assigned a value and has only declared has default value as undefined.
  5. A function which does not return a value returns undefined.
  6. undefined property in global object is non-writable, non-configurable, non-enumerable.
  7. typeof operator when used with undefined returns ‘undefined’.
  8. null is not equivalent to undefined.

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.