Skip to content

sureshalagarsamy/javascript-null-undefined

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

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