小于等于(<=)
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
小于等于运算符(<=
)在左操作数小于等于右操作数时返回 true
,否则返回 false
。
尝试一下
console.log(5 <= 3);
// Expected output: false
console.log(3 <= 3);
// Expected output: true
// Compare bigint to number
console.log(3n <= 5);
// Expected output: true
console.log("aa" <= "ab");
// Expected output: true
语法
js
x <= y
描述
操作数比较使用与小于运算符相同的算法,除了相等的值(在尝试转换后)会返回 true
。
示例
字符串与字符串比较
js
console.log("a" <= "b"); // true
console.log("a" <= "a"); // true
console.log("a" <= "3"); // false
字符串与数值比较
js
console.log("5" <= 3); // false
console.log("3" <= 3); // true
console.log("3" <= 5); // true
console.log("hello" <= 5); // false
console.log(5 <= "hello"); // false
数值与数值比较
js
console.log(5 <= 3); // false
console.log(3 <= 3); // true
console.log(3 <= 5); // true
数值与大整型比较
js
console.log(5n <= 3); // false
console.log(3 <= 3n); // true
console.log(3 <= 5n); // true
比较 Boolean、null、undefined 和 NaN
js
console.log(true <= false); // false
console.log(true <= true); // true
console.log(false <= true); // true
console.log(true <= 0); // false
console.log(true <= 1); // true
console.log(null <= 0); // true
console.log(1 <= null); // false
console.log(undefined <= 3); // false
console.log(3 <= undefined); // false
console.log(3 <= NaN); // false
console.log(NaN <= 3); // false
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-relational-operators |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Less than or equal ( a <= b ) |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.