大於或等於 (>=)
大於或等於 (>=) 運算子在左運算元大於或等於右運算元時返回 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 >= y 和 x < y 都為 false。
- 如果其中一個運算元轉換為 BigInt,而另一個運算元轉換為無法轉換為 BigInt 值的字串(當傳遞給
BigInt()時會丟擲語法錯誤)。 - 如果其中一個運算元轉換為
NaN。(例如,無法轉換為數字的字串,或undefined。)
x >= y 通常等同於 x > y || x == y,除了以下幾種情況:
- 當
x或y中的一個為null,而另一個不是null並且在強制轉換為數字時變為 0(包括0、0n、false、""、"0"、new Date(0)等)時:x >= y為true,而x > y || x == y為false。 - 當
x或y中的一個為undefined,而另一個為null或undefined時:x >= y為false,而x == y為true。 - 當
x和y是同一物件,但在小於操作的第一步之後變為NaN(例如new Date(NaN))時:x >= y為false,而x == y為true。 - 當
x和y是不同的物件,但在小於操作的第一步之後變為相同的值時:x >= y為true,而x > y || x == y為false。
示例
字串與字串比較
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 |
瀏覽器相容性
載入中…