js中的null、空、undefined

 

在任何一門程序語言中判空都是一個繞不過而且比較重要的問題, 而每一門語言的處理又不盡相同,下面以測試案例驅動, 回答其中的不同

以下爲測試過程

var a;
console.log(a);

執行結果:undefined

var b=null;
console.log(b);

執行結果:null

var c=undefined;
console.log(c);

執行結果:undefined

var d="";
console.log(d);

執行結果:undefined

var a;
var b="";
console.log(a==b);

執行結果:true

var a;
var c=undefined;
var b="";
console.log(a==c);
console.log(b==c);

執行結果:false、false

var a=0;
var b="";
console.log(a==b);

執行結果:false

var a;
var b=null;
console.log(a==b);

執行結果:false

var a="";
var b=null;
console.log(a==b);

執行結果:false

var a="";

console.log(a==undefined);

執行結果:false

var b=null;
console.log(b==undefined);

執行結果:true

var c;
console.log(c==undefined);

執行結果:true

console.log(null==undefined);

執行結果:true

console.log(null==null);

執行結果:true

var a="";
var b=null;
console.log(a==b);

執行結果:false

總結:在js中,如果未定義變量或變量爲null,則爲undefined,null、undefined、 未定義的變量三者之間是相等的,但都不等於空

 

 

歡迎志趣相投的朋友一起學習交流! 
本人qq:906570770 
技術資料分享羣:272811256

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章