Null and Undefined in javascript

1.Null and Undefined

In javascript an undefined variable is a variable that never been declared or never assigned a value.

1
2
var foo;
console.log(foo); // The output will be: undefined

If you assign foo = null. Then foo will be null which is a javascript object

1
2
3
4
5
var foo;
console.log(typeof foo); //output: undefined

foo = null;
console.log(typeof foo); //output: object

2.Confuse of working with “==” and “===”.

=== means strictly equal which will check both type and value.

Servral samples:

1
2
3
4
null === null            # => true
undefined === undefined # => true
undefined === null # => false
undefined == null # => true

3.What is false in javascript?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(!false) {
//definitely, false is false
}
if(!undefined) {
//undefined is false
}
if(!null) {
//null is false
}
if(!'') {
//empty string '' is false
}
if(!0) {
//numver 0 is false
}
if(!NaN) {
//numver NaN is false
}

Except these value, others could be regard as true.

ref: http://stackoverflow.com/questions/5101948/javascript-checking-for-null-vs-undefined-and-difference-between-and