大于或等于 (>=)
大于或等于 (>=) 运算符在左操作数大于或等于右操作数时返回 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 |
浏览器兼容性
加载中…