DIV CSS 佈局教程網

用 typeof 判斷 JavaScript 中 undefined 型別
編輯:關於JavaScript     

在 JavaScript 中判斷一個值是否為 undefined,直覺上可能會這樣寫:

 if (reValue== undefined) { alert("undefined"); }

但實測會發現根本判斷不出來。查了資料後才知道,正確做法應該用 typeof:

 if (typeof(reValue) == "undefined") { alert("undefined"); }

關鍵在於,typeof 會回傳一個字串,可能的結果有六種:"number"、"string"、"boolean"、"object"、"function"、"undefined"。因此判斷 undefined 時,用 typeof 搭配字串比較才是可靠的做法。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved