-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
105 lines (72 loc) · 3.43 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
title: Less than or equal (<=)
slug: Web/JavaScript/Reference/Operators/Less_than_or_equal
page-type: javascript-operator
browser-compat: javascript.operators.less_than_or_equal
---
{{jsSidebar("Operators")}}
The **less than or equal (`<=`)** operator returns `true` if the left operand is less than or equal to the right operand, and `false` otherwise.
{{EmbedInteractiveExample("pages/js/expressions-less-than-or-equal.html")}}
## Syntax
```js-nolint
x <= y
```
## Description
The operands are compared using the same algorithm as the [Less than](/en-US/docs/Web/JavaScript/Reference/Operators/Less_than) operator, with the operands swapped and the result negated. `x <= y` is generally equivalent to `!(y < x)`, except for two cases where `x <= y` and `x > y` are both `false`:
- If one of the operands gets converted to a BigInt, while the other gets converted to a string that cannot be converted to a BigInt value (it throws a [syntax error](/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_BigInt_syntax) when passed to [`BigInt()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/BigInt)).
- If one of the operands gets converted to `NaN`. (For example, strings that cannot be converted to numbers, or `undefined`.)
In addition, `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.
`x <= y` is generally equivalent to `x < y || x == y`, except for a few cases:
- When one of `x` or `y` is `null`, and the other is something that's not `null` and becomes 0 when [coerced to numeric](/en-US/docs/Web/JavaScript/Data_structures#numeric_coercion) (including `0`, `0n`, `false`, `""`, `"0"`, `new Date(0)`, etc.): `x <= y` is `true`, while `x < y || x == y` is `false`.
- When one of `x` or `y` is `undefined`, and the other is one of `null` or `undefined`: `x <= y` is `false`, while `x == y` is `true`.
- When `x` and `y` are the same object that becomes `NaN` after the first step of [Less than](/en-US/docs/Web/JavaScript/Reference/Operators/Less_than) (such as `new Date(NaN)`): `x <= y` is `false`, while `x == y` is `true`.
- When `x` and `y` are different objects that become the same value after the first step of [Less than](/en-US/docs/Web/JavaScript/Reference/Operators/Less_than): `x <= y` is `true`, while `x < y || x == y` is `false`.
## Examples
### String to string comparison
```js
"a" <= "b"; // true
"a" <= "a"; // true
"a" <= "3"; // false
```
### String to number comparison
```js
"5" <= 3; // false
"3" <= 3; // true
"3" <= 5; // true
"hello" <= 5; // false
5 <= "hello"; // false
```
### Number to Number comparison
```js
5 <= 3; // false
3 <= 3; // true
3 <= 5; // true
```
### Number to BigInt comparison
```js
5n <= 3; // false
3 <= 3n; // true
3 <= 5n; // true
```
### Comparing Boolean, null, undefined, NaN
```js
true <= false; // false
true <= true; // true
false <= true; // true
true <= 0; // false
true <= 1; // true
null <= 0; // true
1 <= null; // false
undefined <= 3; // false
3 <= undefined; // false
3 <= NaN; // false
NaN <= 3; // false
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- [Greater than (`>`)](/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than)
- [Greater than or equal (`>=`)](/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than_or_equal)
- [Less than (`<`)](/en-US/docs/Web/JavaScript/Reference/Operators/Less_than)