文章最新版地址:http://leeyee.github.io/blog/2013/05/07/javascript-null-undefined
var param; | var param = undefined; | var param = null; | 未定义 | |
param == 'undefined' | false | false | false | 报错。param未定义 |
param === 'undefined' | false | false | false | 报错。param未定义 |
param == undefined | true | true | true | 报错。param未定义 |
param === undefined | true | true | false | 报错。param未定义 |
typeof param | undefined | undefined | object | undefined |
typeof param == 'undefined' | true | true | false | true |
typeof param === 'undefined' | true | true | false | true |
typeof param == undefined | false | false | false | false |
typeof param === undefined | false | false | false | false |
if(param) | false | false | false | 报错。param未定义 |
if(typeof param) | true | true | true | true |
结论:
function test1() { var param;// 定义但未赋值 console.log("1. var param; --> ", "定义但未赋值"); console.log(" 1.1. param == 'undefined' <--> ", param == 'undefined'); // false console.log(" 1.2. param == undefined <--> ", param == undefined);// true console.log(" 1.1.1 param === 'undefined' <--> ", param === 'undefined'); // false console.log(" 1.2.1 param === undefined <--> ", param === undefined);// true console.log(" 1.3. typeof param <--> ", typeof param); // undefined console.log(" 1.4. typeof param == 'undefined' <--> ", typeof param == 'undefined'); //true console.log(" 1.5. typeof param == undefined <--> ", typeof param == undefined);//false console.log(" 1.4.1 typeof param === 'undefined' <--> ", typeof param === 'undefined'); //true console.log(" 1.5.1 typeof param === undefined <--> ", typeof param === undefined);//false param ? console.log(" 1.6. if(param) return true") : console .log(" 1.6. if(param) return false"); typeof param ? console.log(" 1.7. if(typeof param) return true") : console.log(" 1.7. if(typeof param) return false"); } function test2() { console.log("2. param未定义"); console.log(" 2.1. param == 'undefined' <--> ", "报错,param未定义"); console.log(" 2.2. param == undefined <--> ", "报错,param未定义"); console.log(" 2.3. typeof param <--> ", typeof param); // undefined console.log(" 2.4. typeof param == 'undefined' <--> ", typeof param == "undefined");//true console.log(" 2.5. typeof param == undefined <--> ", typeof param == undefined);//false console.log(" 2.4.1 typeof param === 'undefined' <--> ", typeof param === 'undefined');//true console.log(" 2.5.1 typeof param === undefined <--> ", typeof param === undefined);//false console.log(" 2.6. if(param) 由于param未定义,因此报错"); typeof param ? console.log(" 2.7. if(typeof param) return true") : console.log(" 2.8. if(typeof param) return false"); } function test3() { var param = null; console.log("3. var param = null; -->", "定义默认为null"); console.log(" 3.1. param == 'undefined' <--> ", param == 'undefined'); // false console.log(" 3.2. param == undefined <--> ", param == undefined);// true console.log(" 3.1.1 param === 'undefined' <--> ", param === 'undefined'); // false console.log(" 3.2.1 param === undefined <--> ", param === undefined);// true console.log(" 3.3. typeof param <--> ", typeof param); // object console.log(" 3.4. typeof param == 'undefined' <--> ", typeof param == 'undefined'); //false console.log(" 3.5. typeof param == undefined <--> ", typeof param == undefined);//false console.log(" 3.4.1 typeof param === 'undefined' <--> ", typeof param === 'undefined'); //false console.log(" 3.5.1 typeof param === undefined <--> ", typeof param === undefined);//false param ? console.log(" 3.6. if(param) return true") : console .log(" 3.6. if(param) return false"); typeof param ? console.log(" 3.7. if(typeof param) return true") : console.log(" 3.7. if(typeof param) return false"); } function test4() { var param = undefined; console.log("4. var param = undefined; -->", "定义默认为undefined"); console.log(" 4.1. param == 'undefined' <--> ", param == 'undefined'); // false console.log(" 4.2. param == undefined <--> ", param == undefined);// true console.log(" 4.3. typeof param <--> ", typeof param); // undefined console.log(" 4.4. typeof param == 'undefined' <--> ", typeof param == 'undefined'); //true console.log(" 4.5. typeof param == undefined <--> ", typeof param == undefined);//false param ? console.log(" 4.6. if(param) return true") : console .log(" 4.6. if(param) return false"); typeof param ? console.log(" 4.7. if(typeof param) return true") : console.log(" 4.7. if(typeof param) return false"); } test1(); test2(); test3(); test4();
测试结果:
1. var param; --> 定义但未赋值 1.1. param == 'undefined' <--> false 1.2. param == undefined <--> true 1.1.1 param === 'undefined' <--> false 1.2.1 param === undefined <--> true 1.3. typeof param <--> undefined 1.4. typeof param == 'undefined' <--> true 1.5. typeof param == undefined <--> false 1.4.1 typeof param === 'undefined' <--> true 1.5.1 typeof param === undefined <--> false 1.6. if(param) return false 1.7. if(typeof param) return true 2. param未定义 2.1. param == 'undefined' <--> 报错,param未定义 2.2. param == undefined <--> 报错,param未定义 2.3. typeof param <--> undefined 2.4. typeof param == 'undefined' <--> true 2.5. typeof param == undefined <--> false 2.4.1 typeof param === 'undefined' <--> true 2.5.1 typeof param === undefined <--> false 2.6. if(param) 由于param未定义,因此报错 2.7. if(typeof param) return true 3. var param = null; -->定义默认为null 3.1. param == 'undefined' <--> false 3.2. param == undefined <--> true 3.1.1 param === 'undefined' <--> false 3.2.1 param === undefined <--> false 3.3. typeof param <--> object 3.4. typeof param == 'undefined' <--> false 3.5. typeof param == undefined <--> false 3.4.1 typeof param === 'undefined' <--> false 3.5.1 typeof param === undefined <--> false 3.6. if(param) return false 3.7. if(typeof param) return true 4. var param = undefined; -->定义默认为undefined 4.1. param == 'undefined' <--> false 4.2. param == undefined <--> true 4.3. typeof param <--> undefined 4.4. typeof param == 'undefined' <--> true 4.5. typeof param == undefined <--> false 4.6. if(param) return false 4.7. if(typeof param) return true