Undefined Vs Null in JavaScript - GeeksforGeeks

05 Nov, 2020
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: undefinedYou 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 betterunderstanding.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 valuevar temp;
if(temp === undefined)
console.log("true");
else
console.log("false");
Output: trueAccessing values which does not existvar temp=['a','b','c'];
if(temp[3] === undefined)
console.log("true");
else
console.log("false");
Output:true
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.
Definition:
Null: It is the intentional absence of the value. It is one of the primitive values of JavaScript.
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.
Undefined: It means the value does not exist in the compiler. It is the global object.
Type:Null: Object Undefined: undefined
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.
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.
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 betterunderstanding.isNaN(2 +  null)      // false
isNaN(2 + undefined) // true

Differentiating using isNaN():You can refer to NaN article for betterunderstanding.
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 valuevar temp;
if(temp === undefined)
console.log("true");
else
console.log("false");
Output: trueAccessing values which does not existvar temp=['a','b','c'];
if(temp[3] === undefined)
console.log("true");
else
console.log("false");
Output:true
Examples:
Null:null ? console.log("true") : console.log("false") // false
Null is also referred as false.
Null:
null ? console.log("true") : console.log("false") // false

Null is also referred as false.
Undefined:When variable is not assigned a valuevar temp;
if(temp === undefined)
console.log("true");
else
console.log("false");
Output: trueAccessing values which does not existvar temp=['a','b','c'];
if(temp[3] === undefined)
console.log("true");
else
console.log("false");
Output:true
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
bunnyram19
javascript-basics
Difference Between
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference between Process and Thread
Difference Between Method Overloading and Method Overriding in Java
Difference between Clustered and Non-clustered index
Differences between IPv4 and IPv6
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to calculate the number of days between two dates in javascript?
File uploading in React.js