sureshalagarsamy / javascript-null-undefined

Difference between null and undefined

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What are the differences between null and undefined?

Undefined

 var TestVar;
 alert(TestVar); //shows undefined
 alert(typeof TestVar); //shows undefined

null

null is a special keyword that indicates an absence of value.

  • "suresh" is string,
  • true is boolean ,
  • 1234 is number,
  • null is undefined.

null doesn't represent a string that has no value - empty string-

Like

var a = ''; 
console.log(typeof a); // string 
console.log(a == null); //false 
console.log(a == undefined); // false 

Now if

var a;
console.log(a == null); //true
console.log(a == undefined); //true 

But

var a; 
console.log(a === null); //false 
console.log(a === undefined); // true

undefined use it to compare the variable data type

null use it to empty a value of a variable