The Wayback Machine - https://web.archive.org/web/20230226081952/https://www.geeksforgeeks.org/undefined-vs-null-in-javascript/
Skip to content
Related Articles
Open in App
Not now

Related Articles

Undefined Vs Null in JavaScript

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 22 Feb, 2023
Improve Article
Save Article

There are several differences between null and undefined, which are sometimes understood as the same.

Definition:

Null: It is the intentional absence of the value. It is one of the primitive values of JavaScript.

Undefined: It means the value does not exist in the compiler. It is the global object.

Type:

Null: Object 

Undefined: undefined

You can see refer to “==” vs “===” article.

 null == undefined // true
 null === undefined // false

It means null is equal to undefined but not identical.

When we define a variable to undefined then we are trying to convey that the variable does not exist . When we define a variable to null then we are trying to convey that the variable is empty.

Differentiating using isNaN(): You can refer to NaN article for better understanding.

isNaN(2 +  null)      // false
isNaN(2 + undefined) // true

Examples:

Null:

null ? console.log("true") : console.log("false") // false

Null is also referred as false.

Undefined:

When variable is not assigned a value

var temp;
if(temp === undefined)
console.log("true");
else
console.log("false");

Output:

 true

Accessing values which does not exist

var temp=['a','b','c'];
if(temp[3] === undefined)
console.log("true");
else
console.log("false");

Output:

true

Let us see the differences in a tabular form -:

 UndefinedNULL
1.Undefined means a variable has been declared but has yet not been assigned a value.Null is an assignment value. It can be assigned to a variable as a representation of no value. 
2.It is an ECMAScript1 (ES1) feature.It is a Primitive value in Javascript
3.

Its supported browsers are -:

Chrome , Microsoft Edge , Internet Explorer , Firefox , Safari , Opera Mini

Its syntax is -:

null

4.It is a global property.

Its supported browsers are -:

Chrome , Microsoft Edge , Internet Explorer , Firefox , Safari , Opera Mini

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!