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
106
107
108
109
110
|
---
title: Less than (<)
slug: Web/JavaScript/Reference/Operators/Less_than
translation_of: Web/JavaScript/Reference/Operators/Less_than
---
<div>{{jsSidebar("Operators")}}</div>
<p>The less than operator (<code><</code>) returns <code>true</code> if the left operand is less than the right operand, and <code>false</code> otherwise.</p>
<div>{{EmbedInteractiveExample("pages/js/expressions-less-than.html")}}</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox notranslate"> x < y</pre>
<h2 id="Description">Description</h2>
<p>The operands are compared using the <a href="https://tc39.es/ecma262/#sec-abstract-relational-comparison">Abstract Relational Comparison</a> algorithm, which is roughly summarised below:</p>
<ul>
<li>First, objects are converted to primitives using <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive">Symbol.ToPrimitive</a></code> with the <code>hint</code> parameter be <code>'number'</code>.</li>
<li>If both values are strings, they are compared as strings, based on the values of the Unicode code points they contain.</li>
<li>Otherwise JavaScript attempts to convert non-numeric types to numeric values:
<ul>
<li>Boolean values <code>true</code> and <code>false</code> are converted to 1 and 0 respectively.</li>
<li><code>null</code> is converted to 0.</li>
<li><code>undefined</code> is converted to <code>NaN</code>.</li>
<li>Strings are converted based on the values they contain, and are converted as <code>NaN</code> if they do not contain numeric values.</li>
</ul>
</li>
<li>If either value is <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN">NaN</a></code>, the operator returns <code>false</code>.</li>
<li>Otherwise the values are compared as numeric values.</li>
</ul>
<h2 id="Examples">Examples</h2>
<h3 id="String_to_string_comparison">String to string comparison</h3>
<pre class="brush: js notranslate">console.log("a" < "b"); // true
console.log("a" < "a"); // false
console.log("a" < "3"); // false</pre>
<h3 id="String_to_number_comparison">String to number comparison</h3>
<pre class="brush: js notranslate">console.log("5" < 3); // false
console.log("3" < 3); // false
console.log("3" < 5); // true
console.log("hello" < 5); // false
console.log(5 < "hello"); // false
console.log("5" < 3n); // false
console.log("3" < 5n); // true</pre>
<h3 id="Number_to_Number_comparison">Number to Number comparison</h3>
<pre class="brush: js notranslate">console.log(5 < 3); // false
console.log(3 < 3); // false
console.log(3 < 5); // true</pre>
<h3 id="Number_to_BigInt_comparison">Number to BigInt comparison</h3>
<pre class="brush: js notranslate">console.log(5n < 3); // false
console.log(3 < 5n); // true</pre>
<h3 id="Comparing_Boolean_null_undefined_NaN">Comparing Boolean, null, undefined, NaN</h3>
<pre class="brush: js notranslate">console.log(true < false); // false
console.log(false < true); // true
console.log(0 < true); // true
console.log(true < 1); // false
console.log(null < 0); // false
console.log(null < 1); // true
console.log(undefined < 3); // false
console.log(3 < undefined); // false
console.log(3 < NaN); // false
console.log(NaN < 3); // false</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-relational-operators', 'Relational operators')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{Compat("javascript.operators.less_than")}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than">Greater than operator</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Greater_than_or_equal">Greater than or equal operator</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/Less_than_or_equal">Less than or equal operator</a></li>
</ul>
|