null, undefined 和 undeclared 的差異
2024年9月21日•4 分鐘閱讀
null, undefined 和 undeclared 的差異
null
- 變數被宣告並且被指定爲有意義的空值
null
,通常用來明確地表示「沒有值」或「空物件」的狀態。null
的型別是物件。 - 型別:
null
的型別是物件。 - 使用場景:當你明確地想表示某個變數不應該有任何值時,可以將其設為
null
。
1let obj = null;2console.log(typeof obj); // "object"
undefined
- 變數被宣告已經被宣告但是並沒有指派一個值,當 JavaScript 初始化變數或找不到某個物件的屬性時,會自動給它賦值為
undefined
。 - 型別:
undefined
。 - 使用場景:當沒有給變數賦值,或者函數沒有返回值時,JavaScript 會自動給它
undefined
。
在鬆散的比較中, null
== undefined
,但在嚴格比較中 === 它們是不相等的。
1console.log(null == undefined); // true2console.log(null === undefined); // false
1let x;2console.log(x); // undefined3console.log(typeof x); // "undefined"
undeclared
- undeclared 就是從未被宣告過的變數。如果在嚴格模式下嘗試使用未宣告的變數,會拋出 ReferenceError 錯誤。
- 使用場景:undeclared 通常是由於變數使用前未宣告造成的錯誤,這在嚴格模式下尤其會被檢測出來。
1"use strict";23x = 1; //會在嚴格模式下拋出 ReferenceError 錯誤,因為變數沒有被宣告4console.log(x);
在非嚴格模式下,這種行為會自動在全域範圍創建變數,這可能導致意想不到的副作用。
x = 1; // 非嚴格模式下會隱式創建全域變數 x
console.log(x); // 1
實例練習 1
Type Utilities In this question, we will implement the following utility functions to determine the types of primitive values.
- isBoolean(value): Return true if value is a boolean, false otherwise.
- isNumber(value): Return true if value is a number, false otherwise. Note that NaN is considered a number.
- isNull(value): Return true if value is null, false otherwise.
- isString(value): Return true if value is a String, else false.
- isSymbol(value): Return true if value is a Symbol primitive, else false.
- isUndefined(value): Return true if value is undefined, else false.
解題 1
1export function isBoolean(value) {2 return typeof value === "boolean";3}45export function isNumber(value) {6 return typeof value === "number";7}89export function isNull(value) {10 return value === null;11}1213export function isString(value) {14 return typeof value === "string";15}1617export function isSymbol(value) {18 return typeof value === "symbol";19}2021export function isUndefined(value) {22 return typeof value === "undefined";23}
實例練習 2
1// This is a JavaScript Quiz from BFE.dev23console.log(JSON.stringify([1, 2, null, 3]));4console.log(JSON.stringify([1, 2, undefined, 3]));5console.log(null === undefined);6console.log(null == undefined);7console.log(null == 0);8console.log(null < 0);9console.log(null > 0);10console.log(null <= 0);11console.log(null >= 0);12console.log(undefined == 0);13console.log(undefined < 0);14console.log(undefined > 0);15console.log(undefined <= 0);16console.log(undefined >= 0);
解題 2
1// This is a JavaScript Quiz from BFE.dev23console.log(JSON.stringify([1, 2, null, 3])); //"[1,2,null,3]"4console.log(JSON.stringify([1, 2, undefined, 3])); //"[1,2,null,3]",undefined 被轉換為 null,因為 JSON 不支持 undefined 作為有效值5console.log(null === undefined); //false6console.log(null == undefined); //true7console.log(null == 0); //false8console.log(null < 0); //false9console.log(null > 0); //false10console.log(null <= 0); //true,null 會被轉換為 011console.log(null >= 0); //true,null 會被轉換為 012console.log(undefined == 0); //false,undefined 會被轉換為 NaN13console.log(undefined < 0); //false,undefined 會被轉換為 NaN14console.log(undefined > 0); //false,undefined 會被轉換為 NaN15console.log(undefined <= 0); //false,undefined 會被轉換為 NaN16console.log(undefined >= 0); //false,undefined 會被轉換為 NaN
標籤:
2024iT鐵人賽JavaScriptnullundefinedundeclared