# Comparisons

## Boolean is the result

如同其他運算符號，比較運算符號返回一個值，這值為布林值。

```javascript
alert( 2 > 1 );  // true (correct)
alert( 2 == 1 ); // false (wrong)
let result = 5 > 4; // assign the result of the comparison
alert( result ); // true
```

## String comparison

比較 2 個字串的規則：用 Unicode 由左到右比較字母大小，大小寫式有分別，小寫比較大。

## Comparison of different types

比較不同類型，會把值轉換成數字。

```javascript
alert( '2' > 1 ); // true, string '2' becomes a number 2
alert( '01' == 1 ); // true, string '01' becomes a number 1
```

## Strict equality

`==`、`!=` 有問題，會把類型轉換。

```javascript
alert( 0 == false ); // true
```

`===`、`!==` 不會有類型轉換。

```javascript
alert( 0 === false ); // false, because the types are different
```

## Comparison with null and undefined

```javascript
alert( null == undefined ); // true
alert( null === undefined ); // false

alert( null > 0 );  // false ∵ null = 0
alert( null == 0 ); // false ∵ null == undefined
alert( null >= 0 ); // true ∵ null = 0

alert( undefined > 0 ); // false ∵ undefined = NaN
alert( undefined < 0 ); // false ∵ undefined = NaN
alert( undefined == 0 ); // false ∵ null == undefined
```

不需要記得這麼多，如果值可能出現`null`或`undefined`，只要使用`===`，就可以避免複雜情況發生。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mistborn.gitbook.io/til-coding/javascript/comparisons.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
