# Less than (<)

## 在此页

The less than operator ( ``` < ``` ) returns ``` true ``` if the left operand is less than the right operand, and ``` false ``` 否则。

## 句法

``` x < y
```

## 描述

The operands are compared using the Abstract Relational Comparison algorithm, which is roughly summarised below:

• First, objects are converted to primitives using ``` Symbol.ToPrimitive ``` .
• If both values are strings, they are compared as strings, based on the values of the Unicode code points they contain.
• Otherwise JavaScript attempts to convert non-numeric types to numeric values:
• Boolean values ``` true ``` and ``` false ``` are converted to 1 and 0 respectively.
• ``` null ``` is converted to 0.
• ``` undefined ``` is converted to ``` NaN ``` .
• Strings are converted based on the values they contain, and are converted as ``` NaN ``` if they do not contain numeric values.
• If either value is ``` NaN ``` , the operator returns ``` false ``` .
• Otherwise the values are compared as numeric values.

## 范例

### String to string comparison

```console.log("a" < "b");        // true
console.log("a" < "a");        // false
console.log("a" < "3");        // false
```

### String to number comparison

```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
```

### Number to Number comparison

```console.log(5 < 3);            // false
console.log(3 < 3);            // false
console.log(3 < 5);            // true
```

### Number to BigInt comparison

```console.log(5n < 3);           // false
console.log(3 < 5n);           // true
```

### Comparing Boolean, null, undefined, NaN

```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
```

## 规范

ECMAScript (ECMA-262)
The definition of 'Relational operators' in that specification.

## 浏览器兼容性

 Desktop Mobile Server Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Chrome 1 Edge 12 Firefox 1 IE 3 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs 0.1.100

## 另请参阅

1. JavaScript
2. 教程：
3. 完整初学者
4. JavaScript 指南
5. 中间体
6. 高级
7. 参考：
8. 内置对象
9. 表达式 & 运算符
10. 语句 & 声明
11. 函数
12. 错误
13. 杂项