Is Null and Undefined Same in JavaScript?

Null and undefined are values in javascript that are both Primitive Type and immutable (All Primitives Types are immutable)

Unlike other primitive types such as Strings, Numbers, Symbols and Booleans, null and undefined do not have any methods and they are also both falsy values.

So is null and undefined same in javascript?

First let’s check it’s type.In javascript, values have types so what type of value is null and undefined?

Null Type has only one value which is null and similarly Undefined Type also has only one value undefined. So null and undefined are different type of Primitive Type.

Caution

When you use typeof operator to identify primitive type, it will return “undefined” if the value is undefined but it will return “object” if the value is null as shown in the example below.

console.log(typeof undefined);

//"undefined"

console.log(typeof null);

//"object"

This is a historical bug in javascript programming language. Fixing this bug will break codes so this bug will not be rectify.

So, how do you identify null value?

To identify null value, you can compare value you want to check with null with strict equality operator.

value === null;

This will only be true if value is also a null value.

console.log(null===null)
// true

console.log(null===undefined)
// false

console.log(null===false)
// false

console.log(null===0)
// false

console.log(null=== -0)
// false

console.log(null==="")
// false

console.log(null===NaN)      
// false

If you use loose equality operator, null and undefined comparison will be true while rest of the comparison will remain false.

console.log(null==null)
// true

console.log(null==undefined)
// true

console.log(null==false)
// false

console.log(null==0)
// false

console.log(null== -0)
// false

console.log(null==0n)
// false

console.log(null=="")
// false

console.log(null==NaN)
// false

Undefined in Javascript

If you declare a variable and don’t assign any value to it then it will have undefined value. Although variable gets undefined value by default, you can also assign undefined value to a variable but it is not common and you should avoid it. 

If you want to assign non value to variable or reassign variable to non value, always use null instead.

var x;

var y = undefined;

console.log(x);

// undefined

console.log(y);

// undefined

Null in Javascript

Similar to undefined, null also implies absence of value but unlike undefined, null must be assigned to a variable. It is never set as a default value by javascript. It must be done programmatically.

var z = null;

console.log(z);

// undefined

How to distinguish between null and undefined?

var arr = ["red","blue","green"];

console.log(arr[0]);  
//  'red'

console.log(arr[3]);
// undefined

While arr[0] gives you the first element “red”, arr[3] gives you undefined because there is no element. There is a absence of value.

var status = 'active';

var account = 'open';

status = 'inactive';

account = null;

Let’s say we have a program where if user status is active,we will open the account for the user and if the user status changes to inactive, we will set the user account to no value or to nothing. We could code something similar to example above.

Here we are intentionally setting the account to value of nothing i.e we want the account to have no value if account is inactive.

From the examples above we can conclude that undefined is unintended non-existence of value and null is intended non-existence of value.

With null we are saying that we want the variable to be empty and with undefined value javascript language is saying that value you want access does not exists.