javascript null與undefined的區別

來源:互聯網
上載者:User

標籤:type   常用   java   style   區別   ova   原型   col   返回   

undefined表示"缺少值",就是此處應該有一個值,但是還沒有賦值。典型用法是:

(1)變數被聲明了,但沒有賦值時,就等於undefined。

(2) 調用函數時,應該提供的參數沒有提供,該參數等於undefined。

(3)對象沒有賦值的屬性,該屬性的值為undefined。

(4)函數沒有傳回值時,預設返回undefined。

var oValue;  alert(oValue == undefined); //output "true"  

null用來表示尚未存在的對象,常用來表示函數企圖返回一個不存在的對象。典型用法是:

(1) 作為函數的參數,表示該函數的參數不是對象。

(2) 作為對象原型鏈的終點。

alert(null == document.getElementById(‘notExistElement‘));  
alert(typeof undefined);   //output "undefined"  alert(typeof null);        //output "object"  
alert(null == undefined);   //output "true"  

ECMAScript認為undefined是從null派生出來的,所以把它們定義為相等的。

alert(null === undefined); //output "false"  alert(typeof null == typeof undefined); //output "false"  

null與undefined的類型是不一樣的,所以輸出"false"。而‘===’代表絕對等於,在這裡null === undefined輸出false。

javascript null與undefined的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.