Google News
logo
UI Developer - Interview Questions
What is the key difference between Null and Undefined in JavaScript?
Many people think that Null and Undefined are the same in JavaScript, but they have many differences. In the following table, we have included the key differences between them :

Null Undefined
A null is an object with no value. Undefined is a type.
Null is an intentional absence of the value. It is one of the primitive values of JavaScript. In Undefined, the value does not exist in the compiler. It is the global object.
typeof null; // "object" typeof undefined; // "undefined"
Null is equal to undefined but not identical.
null == undefined // true
null === undefined // false
A variable is defined as null when trying to convey that the variable is empty. A variable is defined as undefined when we try to convey that the variable does not exist or is not available.
Null is also referred to as false.
Example :
null ? console.log("true") : console.log("false") //
false
When a variable is not assigned a value, it is called Undefined.
Example :
var temp;
if(temp === undefined)
console.log("true");
else
console.log("false");
Advertisement