getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hint at what coercion is being done

ktkennychow opened this issue · comments


Please type "I already searched for this issue":I already searched for this issue

Edition: 2nd

Book Title: get-started

Chapter: apA

Section Title: Coercive Conditional Comparison

**Question: The third example

var x = "hello";

if (x) {
    // will run!
}

if (x == true) {
    // won't run :(
}

I think it deserves pointing out why the second one doesn't work here, pointing to description point 4 in the MDN doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality can help the reader to further knowledge on what the exact coercion is being done here.

Unless it is intentionally not mentioned to get readers do the research themselves, in that case it is still good to give a hint maybe
**

I could have footnoted to cross-reference this section, which I believe explains what's happening: https://github.com/getify/You-Dont-Know-JS/blob/2nd-ed/get-started/ch2.md#coercive-comparisons

But the intent is that people are reading the appendix AFTER having read the main chapters, so hopefully they're mentally cross-referencing those previous points.

Right, you are not wrong. I did have a fuzzy memory of numeric coercion being a priority, maybe it is not a bad thing to get them to google a bit(like I did) either.