Null and Undefined

Null and Undefined represent the lack of value.

Undefined

When we create a variable but do not assign(分配) any value to it. Its value and data type becomes undefined.

var noValue;//var noValue=""=尚未放值
    console.log(noValue); // undefined
    console.log(typeof noValue); // undefined

Undefined can also appear on other cases, for example when we try to access an index that does not exist in a string:

var name = "Peter";
    console.log(name[0]); // Console will show "P"
    console.log(name[4]); // Console will show "r"
    console.log(name[10]); // Console will show undefined

Null

  1. Null 通常用於重置先前具有值的變量的值:
  2. Null is not exactly a data type and for many people it should be avoided.
var temperature = 35;
    console.log(temperature);

    temperature = null;
    console.log(temperature); // The console will show null
    console.log(typeof temperature); // The console will show object - which could be misleading(誤導)