Difference between undefined and null

In JavaScript, a variable is said to be “undefined” if it has been declared but not initialized. Whereas “null” is assigned to a variable whose value is absent at the time of initialization.

In simple words, when we do not assign any value to a variable, the JavaScript engine treats it as undefined. The null value is assigned by the programmer to indicate that the variable has nothing inside of it, but intends to have a value later in the program execution.

1. JavaScript undefined

Lets see below JavaScript statements to understand the undefined.

var myVar;		//Variable declaration without assigning any value to it

console.log( myVar );	//undefined

console.log( typeof(myVar) );	//undefined

console.log( undeclaredVar );	//Uncaught ReferenceError: undeclaredVar is not defined

If we see a variable with value in undefined then we know that:

  1. The variable has been declared in the program.
  2. There is no value assignment done for the variable.
  3. If we check the type of variable, it will be undefined. Please note that undefined is one of primitive types [string, number, boolean, null, undefined, symbol (new in ES6)] in JavaScript.
  4. If a function returns undefined, it didn’t returned any value.

The undefined is not a reserved keyword in JavaScript.

2. JavaScript null

The null represents intentional absence of value. Conceptually, it’s very much like null in other programming languages like Java, C# etc.

The null expresses a lack of identification, indicating that the variable points to no value or object.

var myVar = null;		//Variable declared and assigned to null

console.log( myVar );	//null

console.log( typeof(myVar) );	//object

Please note that null is of type object with a valid value. The null object has no properties and it is immutable. Note that there can be only a single instance of the null object in the system at a time.

3. Differences between undefined and null Keywords

3.1. Using equality operators with null and undefined

Always remember to use strict equality operator ( === ) for comparing variables with null and undefined, when we want to clearly distinguish between null and undefined variables.

var myUndefinedVar;			//undefined
var myNullVar = null;		//null

myUndefinedVar == myNullVar;		//true - which not correct as both are different	

myUndefinedVar === myNullVar;		//false - correct behavior

3.2. null evaluates to zero

One major difference between null and undefined is in the way they are converted to primitive types.

  • null is converted to zero (0) while performing primitive operations.
  • undefined is converted to NaN.
var myUndefinedVar;			//undefined
var myNullVar = null;		//null

myUndefinedVar + 100;		//NaN

myNullVar + 100;			//100

Happy Learning !!

Leave a Reply

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial