相等 (==)

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

相等 (==) 運算子檢查其兩個運算元是否相等,並返回一個布林結果。與嚴格相等運算子不同,它會嘗試轉換並比較不同型別的運算元。

試一試

console.log(1 == 1);
// Expected output: true

console.log("hello" == "hello");
// Expected output: true

console.log("1" == 1);
// Expected output: true

console.log(0 == false);
// Expected output: true

語法

js
x == y

描述

相等運算子 (==!=) 提供 IsLooselyEqual 語義。這可以大致總結如下:

  1. 如果運算元具有相同的型別,則按以下方式進行比較:
    • 物件:僅當兩個運算元引用同一物件時才返回 true
    • 字串:僅當兩個運算元具有相同字元且順序相同時才返回 true
    • 數字:僅當兩個運算元具有相同的值時才返回 true+0-0 被視為相同的值。如果任一運算元為 NaN,則返回 false;因此,NaN 從不等於 NaN
    • 布林值:僅當兩個運算元都為 true 或都為 false 時才返回 true
    • BigInt:僅當兩個運算元具有相同的值時才返回 true
    • Symbol:僅當兩個運算元引用同一 Symbol 時才返回 true
  2. 如果其中一個運算元為 nullundefined,則另一個運算元也必須為 nullundefined 才能返回 true。否則返回 false
  3. 如果其中一個運算元是物件,而另一個是原始值,則將物件轉換為原始值
  4. 在此步驟中,兩個運算元都將轉換為原始值(字串、數字、布林值、Symbol 和 BigInt 之一)。其餘轉換逐案進行。
    • 如果它們屬於同一型別,則使用步驟 1 進行比較。
    • 如果其中一個運算元是 Symbol 而另一個不是,則返回 false
    • 如果其中一個運算元是布林值而另一個不是,則將布林值轉換為數字true 轉換為 1,false 轉換為 0。然後再次鬆散地比較這兩個運算元。
    • 數字轉換為字串:將字串轉換為數字。轉換失敗將導致 NaN,這將保證相等性為 false
    • 數字轉換為 BigInt:按其數學值進行比較。如果數字是 ±Infinity 或 NaN,則返回 false
    • 字串轉換為 BigInt:使用與 BigInt() 建構函式相同的演算法將字串轉換為 BigInt。如果轉換失敗,則返回 false

鬆散相等是對稱的:對於任何 AB 的值(除了應用轉換的順序),A == B 始終具有與 B == A 相同的語義。

此運算子與嚴格相等 (===) 運算子之間最顯著的區別是,嚴格相等運算子不嘗試型別轉換。相反,嚴格相等運算子始終認為不同型別的運算元是不同的。嚴格相等運算子本質上只執行步驟 1,然後對所有其他情況返回 false

上述演算法有一個“故意違反”的情況:如果其中一個運算元是 document.all,則將其視為 undefined。這意味著 document.all == nulltrue,但 document.all === undefined && document.all === nullfalse

示例

不帶型別轉換的比較

js
1 == 1; // true
"hello" == "hello"; // true

帶型別轉換的比較

js
"1" == 1; // true
1 == "1"; // true
0 == false; // true
0 == null; // false
0 == undefined; // false
0 == !!null; // true, look at Logical NOT operator
0 == !!undefined; // true, look at Logical NOT operator
null == undefined; // true

const number1 = new Number(3);
const number2 = new Number(3);
number1 == 3; // true
number1 == number2; // false

物件的比較

js
const object1 = {
  key: "value",
};

const object2 = {
  key: "value",
};

console.log(object1 == object2); // false
console.log(object1 == object1); // true

比較字串和 String 物件

請注意,使用 new String() 構造的字串是物件。如果將其中一個與字串字面量進行比較,則 String 物件將轉換為字串字面量,並比較其內容。但是,如果兩個運算元都是 String 物件,則它們作為物件進行比較,並且必須引用同一物件才能成功比較。

js
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

比較日期和字串

js
const d = new Date("1995-12-17T03:24:00");
const s = d.toString(); // for example: "Sun Dec 17 1995 03:24:00 GMT-0800 (Pacific Standard Time)"
console.log(d == s); // true

比較陣列和字串

js
const a = [1, 2, 3];
const b = "1,2,3";
a == b; // true, `a` converts to string

const c = [true, 0.5, "hey"];
const d = c.toString(); // "true,0.5,hey"
c == d; // true

規範

規範
ECMAScript® 2026 語言規範
# sec-equality-operators

瀏覽器相容性

另見