What is the difference between null and undefined in JavaScript?

http://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript

There is one difference between undefined And null in javascript :: undefined is a data type and null is a value
defined empty variable is null of datatype undefined
Both of them are representing a value of a variable with no value
AND null doesn't represent a string that has no value
Like
var a = ''; 
console.log(typeof a); // string 


var a = ''  ;
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
SO each one has it own way to use
undefined use it to compare the variable data type
null use it to empty a value of a variable
var a = 'javascript';
a = null ; // will change the type of variable "a" from string to undefined 

评论

此博客中的热门博文

qtCreator + Eclipse

raspberry-qt Compile and hello world