Javascript中NaN、null和undefinded的區別

來源:互聯網
上載者:User

標籤:資料   number   自己   參考型別   not   賦值   代碼   javascrip   typeof   

var a1;var a2 = true;var a3 = 1;var a4 = "Hello";var a5 = new Object();var a6 = null;var a7 = NaN;var a8 = undefined;alert(typeof a);   //顯示"undefined"alert(typeof a1); //顯示"undefined"alert(typeof a2); //顯示"boolean"alert(typeof a3); //顯示"number"alert(typeof a4); //顯示"string"alert(typeof a5); //顯示"object"alert(typeof a6); //顯示"object"alert(typeof a7); //顯示"number"alert(typeof a8); //顯示"undefined"

Js中的資料類型有undefined,boolean,number,string,object等5種,前4種為原始類型,第5種為參考型別。

先看這個上面這個例子,未定義的值和定義未賦值的為undefined,null是一種特殊的object,NaN是一種特殊的number(Not a number)。

var a1;        //a1的值為undefinedvar a2 = null;var a3 = NaN;alert(a1 == a2); //顯示"true"alert(a1 != a2); //顯示"false"alert(a1 == a3); //顯示"false"alert(a1 != a3); //顯示"true"alert(a2 == a3); //顯示"false"alert(a2 != a3); //顯示"true"alert(a3 == a3); //顯示"false"alert(a3 != a3); //顯示"true"

  從上面的代碼可以得出結論:(1)undefined與null是相等;(2)NaN與任何值都不相等,與自己也不相等。

null是對象,表示一個null 指標,undefinded是表示未定義的類型,它的類型本身就叫做”未定義類型“(和object類型的區別)

Javascript中NaN、null和undefinded的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.