[javascript] undefined 與 not defined

在 javascript 中,undefined 是一個特殊值,並不是真的代表沒宣告。

我們可以從以下程式碼來看

var a;
if(a === undefined)
    console.log("a is undefined");
else
    console.log("a is defined");
// a is undefined

再來我們看有指派值給 a 變數

var a="Hello world";
if(a === undefined)
    console.log("a is undefined");
else
    console.log("a is defined");
// a is defined

再來我們將 a 指派為 undefined

var a="Hello world"
a=undefined;
if(a === undefined)
    console.log("a is undefined");
else
    console.log("a is defined");
// a is undefined

由此我們可以發現 undefined 是一個 javascript 的特殊值,代表 undefined 這個值

參考資料:

https://pjchender.blogspot.tw/2015/12/javascript-undefinedundefined.html

Leave a Reply

Your email address will not be published. Required fields are marked *