大于 (>)

如果左操作数大于右操作数,则大于 (>) 运算符返回 true,否则返回 false

¥The greater than (>) operator returns true if the left operand is greater than the right operand, and false otherwise.

Try it

语法

¥Syntax

js
x > y

描述

¥Description

使用与 少于 运算符相同的算法来比较操作数,只不过交换了两个操作数。x > y 通常等同于 y < x,只不过 x > yy 之前将 x 强制为原语,而 y < xx 之前将 y 强制为原语。由于强制转换可能会产生副作用,因此操作数的顺序可能很重要。

¥The operands are compared using the same algorithm as the Less than operator, except the two operands are swapped. x > y is generally equivalent to y < x, except that x > y coerces x to a primitive before y, while y < x coerces y to a primitive before x. Because coercion may have side effects, the order of the operands may matter.

示例

¥Examples

字符串与字符串的比较

¥String to string comparison

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

字符串与数字的比较

¥String to number comparison

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

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

"5" > 3n; // true
"3" > 5n; // false

数字与数字的比较

¥Number to Number comparison

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

数字与 BigInt 比较

¥Number to BigInt comparison

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

比较布尔值、null、未定义、NaN

¥Comparing Boolean, null, undefined, NaN

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

true > 0; // true
true > 1; // false

null > 0; // false
1 > null; // true

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

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

规范

Specification
ECMAScript Language Specification
# sec-relational-operators

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看