Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Add comparing object value to boolean in Truthy/Falsy section. (mbeau…
Browse files Browse the repository at this point in the history
…dru#85)

* add truthy/falsy gotcha

* fixed toprimitive  spectification link

* fixed gramma
  • Loading branch information
muitsfriday authored and mbeaudru committed Oct 29, 2017
1 parent 2483a4b commit 5f2b201
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1540,6 +1540,14 @@ myVar ? "truthy" : "falsy"

myVar is evaluated in a boolean context.

Be careful when comparing 2 values. The object values (that should be cast to true) is **not** being casted to Boolean but it forced to convert into a primitive value one using [ToPrimitives specification](http://javascript.info/object-toprimitive). Internally, when an object is compared to Boolean value like `[] == true`, it does `[].toString() == true` so...

```js
let a = [] == true // a is false since [].toString() give "" back.
let b = [1] == true // b is true since [1].toString() give "1" back.
let c = [2] == true // c is false since [2].toString() give "2" back.
```

#### External resources

- [Truthy (MDN)](https://developer.mozilla.org/en-US/docs/Glossary/Truthy)
Expand Down

0 comments on commit 5f2b201

Please sign in to comment.