JS equality(==,相等運算子,也稱 loose equality)用來檢查兩個運算元是否相等,回傳布林值。兩邊型別不同時,它會先嘗試把運算元轉成同一種型別再比較,這是它和嚴格相等(===)最大的差別。
console.log(1 == 1); // true
console.log("hello" == "hello"); // true
console.log("1" == 1); // true
console.log(0 == false); // true
嚴格相等不轉換型別,請參考嚴格相等(===)的語法文。本篇說明 == 的型別轉換規則。
語法
x == y
x != y
== 與 != 使用同一套比較規則(ECMAScript 稱為 IsLooselyEqual),x == y 為 true 代表依這套規則兩者相等,否則是 false。!= 的結果剛好相反。
比較規則
依 MDN 說明,比較分成下列幾步,由上往下判斷:
- 兩邊型別相同:直接比較,規則和
===一樣。物件要指向同一個物件;字串要字元與順序都相同;數字要數值相同(+0與-0視為相同,只要有NaN就是false);布林、BigInt、Symbol 也各有對應的比較方式。 - 其中一邊是
null或undefined:另一邊也必須是null或undefined才會是true,否則是false。 - 一邊是物件、另一邊是原始值:先把物件轉成原始值。
- 兩邊都是原始值但型別不同:依下表逐項轉換。
| 兩邊的型別 | 處理方式 | 範例 |
|---|---|---|
| 一邊是 Symbol,另一邊不是 | 直接回傳 false | Symbol("x") == "x" |
| 一邊是布林,另一邊不是 | 布林先轉成數字(true 為 1、false 為 0),再重新比較 | true == "1" |
| 數字與字串 | 字串轉成數字再比;轉換失敗得到 NaN,結果一定是 false | "1" == 1 |
| 數字與 BigInt | 比數學上的值;數字是 ±Infinity 或 NaN 時是 false | 1n == 1 |
| 字串與 BigInt | 字串依 BigInt() 建構函式的規則轉成 BigInt;轉換失敗是 false | "1" == 1n |
null 與 undefined
null 與 undefined 只和彼此相等,不會和 0、空字串、false 相等:
console.log(null == undefined); // true
console.log(null == null); // true
console.log(0 == null); // false
console.log(0 == undefined); // false
console.log(null == false); // false
console.log(undefined == ""); // false
這來自上面的第 2 步:只要一邊是 null 或 undefined,就不會走轉換成數字的路徑。兩者各自的說明請參考 null 與 undefined 的語法文。
字串與數字
字串和數字比較時,字串會被轉成數字。MDN 的數字轉換說明指出,字串依數字常值的規則解析,失敗得到 NaN,前後的空白與換行字元會被忽略,空字串或只有空白的字串會轉成 0:
console.log("1" == 1); // true
console.log(" 42 " == 42); // true
console.log("" == 0); // true
console.log("abc" == 0); // false
console.log("abc" == NaN); // false
兩邊都是字串時不會轉換成數字,直接比字元:
console.log("0" == ""); // false
console.log("0" == 0); // true
console.log(0 == ""); // true
所以 "0" == 0、0 == "" 都是 true,但 "0" == "" 是 false,== 並不具備傳遞性(transitivity)。
布林
布林和其他型別比較時,會先轉成數字,再重新比較一次。這代表布林不是直接和字串或物件比較:
console.log(true == 1); // true
console.log(false == 0); // true
console.log(true == "1"); // true
console.log(true == 2); // false
console.log("true" == true); // false
console.log("0" == false); // true
"true" == true 是 false:true 先變成 1,再和字串 "true" 比較,字串轉成數字得到 NaN,所以不相等。
物件與原始值
物件和原始值比較時,物件會先轉成原始值(primitive coercion)。MDN 說明的順序是:呼叫物件的 [Symbol.toPrimitive]()(hint 為 "default"),沒有的話呼叫 valueOf(),再呼叫 toString()。
一般物件和陣列的 valueOf() 回傳的是物件本身,會被忽略,所以實際用到的是 toString():
const a = [1, 2, 3];
const b = "1,2,3";
console.log(a == b); // true
console.log([] == ""); // true
console.log([] == 0); // true
console.log([] == false); // true
console.log({} == "[object Object]"); // true
[] == 0 的過程是:[] 轉成原始值得到空字串,空字串再轉成數字 0,所以是 true。
包裝物件
用 new 建立的 Number、String 是物件。它們和原始值比較時會被轉成原始值;兩邊都是物件時則比參照:
const number1 = new Number(3);
const number2 = new Number(3);
console.log(number1 == 3); // true
console.log(number1 == number2); // false
const string1 = "hello";
const string2 = String("hello");
const string3 = new String("hello");
const string4 = new String("hello");
console.log(string1 == string2); // true
console.log(string1 == string3); // true
console.log(string2 == string3); // true
console.log(string3 == string4); // false
console.log(string4 == string4); // true
Date
MDN 指出,Date 物件是內建物件中覆寫 [Symbol.toPrimitive]() 的其中一個,它把 "default" 當成 "string" 處理。所以 Date 和字串比較時會用日期字串;和數字比較時,是日期字串轉成數字(NaN),結果是 false:
const d = new Date("1995-12-17T03:24:00");
console.log(d == d.toString()); // true
console.log(d == d.getTime()); // false
自訂轉換順序
物件可以自己定義轉換方式。有 [Symbol.toPrimitive]() 時它優先;只有 valueOf() 與 toString() 時,先用 valueOf():
const hints = [];
const o = {
[Symbol.toPrimitive](hint) {
hints.push(hint);
return 42;
},
};
console.log(o == 42); // true
console.log(o == "42"); // true
console.log(hints.join(",")); // default,default
const v = {
valueOf() { return 1; },
toString() { return "2"; },
};
console.log(v == 1); // true
console.log(v == "2"); // false
console.log(v == "1"); // true
v == "2" 是 false,因為先用的是 valueOf() 得到 1,再和 "2" 比較。
[Symbol.toPrimitive]() 如果回傳物件,會丟出 TypeError:
const bad = {
[Symbol.toPrimitive]() {
return {};
},
};
try {
bad == 1;
} catch (e) {
console.log(e.name); // TypeError
}
BigInt 與 Symbol
BigInt 依數學上的值和數字比較,也可以和能轉成 BigInt 的字串比較。Symbol 只能和自己(同一個 Symbol)相等,和其他型別比較一律是 false:
console.log(1n == 1); // true
console.log("1" == 1n); // true
console.log(1n == "abc"); // false
console.log(NaN == 1n); // false
console.log(Symbol("x") == "x"); // false
常見比較結果
下表整理前面範例中容易搞混的結果:
| 運算式 | 結果 | 原因 |
|---|---|---|
"" == 0 | true | 空字串轉成 0 |
"0" == false | true | false 轉成 0,"0" 轉成 0 |
null == 0 | false | null 只和 null、undefined 相等 |
undefined == null | true | 兩者互相相等 |
NaN == NaN | false | 有 NaN 一律是 false |
[] == false | true | false 轉成 0,[] 轉成 "" 再轉成 0 |
[1, 2] == [1, 2] | false | 兩邊都是物件,比的是參照 |
對稱性與 !=
MDN 指出,A == B 和 B == A 的語意相同,只是轉換的先後順序可能不同:
console.log("1" == 1); // true
console.log(1 == "1"); // true
console.log([] == 0); // true
console.log(0 == []); // true
!= 是 == 的相反,同樣會做型別轉換:
console.log(1 != "1"); // false
console.log(1 != 2); // true
console.log(null != undefined); // false
document.all 的例外
MDN 提到,演算法有一個刻意的例外:document.all 在 == 比較時被當成 undefined。所以 document.all == null 是 true,但 document.all === undefined || document.all === null 是 false。這是瀏覽器才有的物件,Node.js 沒有。
和 === 的差別
=== 基本上只做上面的第 1 步,型別不同就是 false,其他情況都不轉換。
console.log("1" == 1); // true
console.log("1" === 1); // false
console.log(0 == false); // true
console.log(0 === false); // false
MDN 指出,多數情況不建議使用 ==:用嚴格相等的結果比較容易預測,也可能因為沒有型別轉換而比較快。
注意事項
- 兩邊型別不同會轉換:
"1" == 1、0 == false都是true。 - 沒有傳遞性:
"0" == 0、0 == ""為true,但"0" == ""是false。 null與undefined只和彼此相等:null == 0是false。- 物件比較看參照:內容一樣的兩個物件,
==也是false。 - 布林先轉成數字:
"true" == true是false。
支援版本
依 MDN 瀏覽器相容性資料(browser-compat-data 8.1.10,查詢日期 2026-11-24):== 從 Chrome 1、Edge 12、Firefox 1、Safari 1 開始支援,Node.js 從 0.10.0 開始,Deno 從 1.0 開始,Internet Explorer 從 3 開始。
補充說明:範例在 Node.js 執行;
document.all的結果在 Chrome 158 以 Playwright 執行確認。MDN 的範例是前面的1 == 1四行、null 與0的比較、包裝物件、String物件、Date與字串、陣列與字串,其餘是依 MDN 的比較規則另外示範的。
發佈留言