Skip to content

Commit 0242818

Browse files
committed
feat: Non-strict comparison of a number to true. Close #135
1 parent de081e4 commit 0242818

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Currently, there are these translations of **wtfjs**:
115115
- [Why you should use semicolons](#why-you-should-use-semicolons)
116116
- [Split a string by a space](#split-a-string-by-a-space)
117117
- [A stringified string](#a-stringified-string)
118+
- [Non-strict comparison of a number to `true`](#non-strict-comparison-of-a-number-to-true)
118119
- [📚 Other resources](#-other-resources)
119120
- [🎓 License](#-license)
120121

@@ -2050,6 +2051,37 @@ It is actually a stringified string, so it's true:
20502051

20512052
- [ECMA-404 The JSON Data Interchange Standard.](https://www.json.org/json-en.html)
20522053

2054+
## Non-strict comparison of a number to `true`
2055+
2056+
```js
2057+
1 == true // -> true
2058+
// but…
2059+
Boolean(1.1) // -> true
2060+
1.1 == true // -> false
2061+
```
2062+
2063+
### 💡 Explanation:
2064+
2065+
According to the specification:
2066+
2067+
> The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
2068+
>
2069+
> 4. If `Type(x)` is Number and `Type(y)` is String, return the result of the comparison `x == ! ToNumber(y)`.
2070+
2071+
So this comparison is performed like this:
2072+
2073+
```js
2074+
1 == true
2075+
1 == Number(true)
2076+
1 == 1 // -> true
2077+
// but…
2078+
1.1 == true
2079+
1.1 == Number(true)
2080+
1.1 == 1 // -> false
2081+
```
2082+
2083+
- [**7.2.15** Abstract Equality Comparison](https://262.ecma-international.org/11.0/index.html#sec-abstract-equality-comparison)
2084+
20532085
# 📚 Other resources
20542086

20552087
- [wtfjs.com](http://wtfjs.com/) — a collection of those very special irregularities, inconsistencies and just plain painfully unintuitive moments for the language of the web.

0 commit comments

Comments
 (0)