js中使用0 “” null undefined {}需要注意
生活随笔
收集整理的這篇文章主要介紹了
js中使用0 “” null undefined {}需要注意
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注意:在js中0為空(false) ,代表空的還有“”,null ,undefined;
如果做判斷if(!上面的四種值);返回均為false
console.log(!null);// true console.log(!0);//true console.log(!"");// true console.log(!undefined);// true console.log(0=="");//true console.log(0==" ");// true console.log(undefined==null);// true----------------------------------------------
var val=0; //undefined console.log(val=='');//true var val='0'; //undefined console.log(val=='');//false console.log(0==undefined);//false console.log('0'==undefined);//false----------------------------------------------
在對接收的參數進行判斷時一定注意 :當參數取值既有0也有‘’時要小心區分
----------------------------------------------- console.log(typeof null) //object console.log(typeof {}) //object console.log(null == {}) //falsevar tem1 = null; var tem2 = {}; console.log(tem2.length); //undefined console.log(tem1.length); //報錯Cannot read property 'length' of null如何判斷是否為空對象。 console.log(tem1==null);//true,因此可以用該方法判斷為null對象 console.log(tem2=={});//false,不能這樣判斷{}對象 應該用下面的方式 console.log(JSON.stringify(tem2) == "{}")//ture-------------------------------------------
?
轉載于:https://www.cnblogs.com/xhliang/p/7464123.html
總結
以上是生活随笔為你收集整理的js中使用0 “” null undefined {}需要注意的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql查询优化之一:mysql查询优
- 下一篇: (转载)20分钟读懂程序集