|
1 | 1 | # About |
2 | 2 |
|
3 | | -TODO: this is a stub that needs more information |
| 3 | +## Comparing Numbers |
4 | 4 |
|
5 | | -## Comparing numbers |
| 5 | +In JavaScript numbers can be compared using the following relational and equality operators. |
6 | 6 |
|
7 | | -Numbers are considered equal if they have the same value. |
| 7 | +| Comparison | Operator | |
| 8 | +| ---------------------- | --------- | |
| 9 | +| Greater than | `a > b` | |
| 10 | +| Greater than or equals | `a >= b` | |
| 11 | +| Less than | `a < b` | |
| 12 | +| Less than or equals | `a <= b` | |
| 13 | +| (Strict) Equals | `a === b` | |
| 14 | +| Not (strict) equals | `a !== b` | |
| 15 | + |
| 16 | +The result of the comparison is always a boolean value, so either `true` or `false`. |
8 | 17 |
|
9 | 18 | ```javascript |
10 | | -1 == 1.0; |
| 19 | +1 < 3; |
11 | 20 | // => true |
12 | 21 |
|
| 22 | +2 !== 2; |
| 23 | +// => false |
| 24 | + |
13 | 25 | 1 === 1.0; |
14 | 26 | // => true |
15 | | -// Remember, all numbers are floating-points, so this is different syntax for |
| 27 | +// All numbers are floating-points, so this is different syntax for |
16 | 28 | // the exact same value. |
| 29 | +``` |
17 | 30 |
|
18 | | -1 === 1n; |
| 31 | +## Comparing Strings |
| 32 | + |
| 33 | +In JavaScript the comparison operators above can also be used to compare strings. |
| 34 | +In that case a dictionary (lexicographical) order is applied. |
| 35 | +You can find a list of the exact order of all the characters [here][utf-16-list]. |
| 36 | + |
| 37 | +```javascript |
| 38 | +'Apple' > 'Pear'; |
19 | 39 | // => false |
20 | | -// Strictly checking a number against a bigint will always result in false. |
21 | 40 |
|
22 | | -1 == 1n; |
| 41 | +'a' < 'above'; |
23 | 42 | // => true |
24 | | -// A number is equal to a bigint if they represent the same value. |
25 | 43 |
|
26 | | -1.0 == 1n; |
| 44 | +'a' === 'A'; |
| 45 | +// => false |
| 46 | +``` |
| 47 | + |
| 48 | +You need to be careful when you compare two variables that appear to contain numeric values but are of type string. |
| 49 | +Due to the dictionary order the result will not be the same as comparing values of type number. |
| 50 | + |
| 51 | +```javascript |
| 52 | +10 < 2; |
| 53 | +// => false |
| 54 | + |
| 55 | +'10' < '2'; |
| 56 | +// => true (because "1" comes before "2") |
| 57 | +``` |
| 58 | + |
| 59 | +Another way to compare strings is the [localeCompare][mdn-locale-compare] method. |
| 60 | +It allows to set a variety of [options][mdn-locale-compare-options] to adjust the way strings are compared. |
| 61 | + |
| 62 | +## Strict Equality |
| 63 | + |
| 64 | +You might wonder about the three equal signs for checking equality in JavaScript. |
| 65 | +`===` represents the check for _strict equality_ which means that no type conversion is performed and values of different types are always unequal. |
| 66 | + |
| 67 | +```javascript |
| 68 | +'3' === 3; |
| 69 | +// => false |
| 70 | +// The value on the left has type string, the value on the right has type number. |
| 71 | + |
| 72 | +1 === 1n; |
| 73 | +// => false |
| 74 | +// The value on the left has type number, the value on the right has type bigint. |
| 75 | +``` |
| 76 | + |
| 77 | +Using `===` and `!==` is the recommended way of checking equality in JavaScript. |
| 78 | + |
| 79 | +## Avoiding Implicit Type Conversion |
| 80 | + |
| 81 | +There is also `==` and `!=` which represents checking for _loose equality_. |
| 82 | +You should avoid it because it will apply implicit type conversion before performing the comparison. |
| 83 | +The outcomes in these cases are hard to predict and sometimes not what you would expect. |
| 84 | +You can read more about it [here][mdn-loose-equals]. |
| 85 | + |
| 86 | +```javascript |
| 87 | +0 == false; |
27 | 88 | // => true |
28 | | -// A number is equal to a bigint if they represent the same value. |
29 | 89 | ``` |
30 | 90 |
|
31 | | -There are different outcomes when comparing numbers with different types. |
32 | | -In general, only two operands of the type `number` can ever be _strictly equal_ (`===`), and the following can be used for _loose equality_ (`==`): |
| 91 | +In theory you can also compare values of different types (e.g., `"1" < 2`). |
| 92 | +Then the values will be implicitly converted to determine whether the result is true or false. |
| 93 | +Just as checking for loose equality, this is also not recommended for the same reason as mentioned above. |
33 | 94 |
|
34 | | -| A | B | `==` | |
35 | | -| ------ | --------- | --------------------- | |
36 | | -| Number | Undefined | `false` | |
37 | | -| Number | Null | `false` | |
38 | | -| Number | Number | `A === B` | |
39 | | -| Number | String | `A === ToNumber(B)` | |
40 | | -| Number | Boolean | `A === ToNumber(B)` | |
41 | | -| Number | Object | `A == ToPrimitive(B)` | |
| 95 | +What should you do instead? |
| 96 | +You can apply [explicit type conversion][concept-type-conversion]. |
| 97 | +With that you can then ensure values have the correct type before performing the comparison. |
| 98 | +Then your code will be easier to understand and less error prone. |
42 | 99 |
|
43 | | -- `ToNumber(X)` attempts to convert its argument `X` to a `number` before comparison. It is equivalent to `+B` (the unary `+` operator). |
44 | | -- `ToPrimitive(X)` attempts to convert its object argument `X` to a primitive value, by attempting to invoke varying sequences of `X.toString` and `X.valueOf` methods on `X`. |
| 100 | +[mdn-loose-equals]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality |
| 101 | +[concept-type-conversion]: /tracks/javascript/concepts/type-conversion |
| 102 | +[utf-16-list]: https://www.fileformat.info/info/charset/UTF-16/list.htm |
| 103 | +[mdn-locale-compare]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare |
| 104 | +[mdn-locale-compare-options]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator#parameters |
0 commit comments