大於或等於 (>=)

Baseline 已廣泛支援

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

大於或等於 (>=) 運算子在左運算元大於或等於右運算元時返回 true,否則返回 false

試一試

console.log(5 >= 3);
// Expected output: true

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

// Compare bigint to number
console.log(3n >= 5);
// Expected output: false

console.log("ab" >= "aa");
// Expected output: true

語法

js
x >= y

描述

運算元使用與小於運算子相同的演算法進行比較,結果取反。x >= y 通常等同於 !(x < y),除了兩種情況下 x >= yx < y 都為 false

  • 如果其中一個運算元轉換為 BigInt,而另一個運算元轉換為無法轉換為 BigInt 值的字串(當傳遞給 BigInt() 時會丟擲語法錯誤)。
  • 如果其中一個運算元轉換為 NaN。(例如,無法轉換為數字的字串,或 undefined。)

x >= y 通常等同於 x > y || x == y,除了以下幾種情況:

  • xy 中的一個為 null,而另一個不是 null 並且在強制轉換為數字時變為 0(包括 00nfalse"""0"new Date(0) 等)時:x >= ytrue,而 x > y || x == yfalse
  • xy 中的一個為 undefined,而另一個為 nullundefined 時:x >= yfalse,而 x == ytrue
  • xy 是同一物件,但在小於操作的第一步之後變為 NaN(例如 new Date(NaN))時:x >= yfalse,而 x == ytrue
  • xy 是不同的物件,但在小於操作的第一步之後變為相同的值時:x >= ytrue,而 x > y || x == yfalse

示例

字串與字串比較

js
"a" >= "b"; // false
"a" >= "a"; // true
"a" >= "3"; // true

字串與數字比較

js
"5" >= 3; // true
"3" >= 3; // true
"3" >= 5; // false

"hello" >= 5; // false
5 >= "hello"; // false

數字與數字比較

js
5 >= 3; // true
3 >= 3; // true
3 >= 5; // false

數字與 BigInt 比較

js
5n >= 3; // true
3 >= 3n; // true
3 >= 5n; // false

比較布林值、null、undefined、NaN

js
true >= false; // true
true >= true; // true
false >= true; // false

true >= 0; // true
true >= 1; // true

null >= 0; // true
1 >= null; // true

undefined >= 3; // false
3 >= undefined; // false

3 >= NaN; // false
NaN >= 3; // false

規範

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

瀏覽器相容性

另見