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
defined empty variable is
Both of them are representing a value of a variable with no value
AND
Like
undefined use it to compare the variable data type
null use it to empty a value of a variable
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
BUTvar 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
评论