impatient-js icon indicating copy to clipboard operation
impatient-js copied to clipboard

Chapter: Operators

Open rauschma opened this issue 6 years ago • 11 comments

rauschma avatar Jun 26 '18 16:06 rauschma

12.4.3.2. Use case for ==: comparing with undefined or null ... I prefer either of the following two alternatives: if (x === undefined || x === null) ···
if (x) ···

Shouldn't it be if (!x) instead of if (x)?

VernonHawk avatar May 07 '19 10:05 VernonHawk

@VernonHawk: Indeed! Thanks for catching this. It’ll be fixed in the next release.

rauschma avatar May 08 '19 02:05 rauschma

Hello @rauschma , Thanks for the great book! I have a question here:

12.4.3.2 Use case for ==: comparing with undefined or null ... I prefer either of the following two alternatives: if (x === undefined || x === null) ··· if (!x) ···

Why the second option is considered as an alternative?

The use case says that check if a value x is either undefined or null, but we know that !x also produces true for other x values like false or 0. I assumed that the use case only allows value x to be either undefined or null, other than those 2 values, it should reject.

echamudi avatar Jul 11 '19 13:07 echamudi

@ezhmd Immediately after that example, there is a link to https://exploringjs.com/impatient-js/ch_booleans.html#falsiness-truthiness It is all explained there.

rauschma avatar Jul 11 '19 13:07 rauschma

@rauschma What I'm thinking is: Yes, x === undefined || x === null is a valid alternative for x == null, because they behave similarly. But !x shouldn't be categorized as an alternative for x == null, because they behave differently, !x accepts more values as explained.

echamudi avatar Jul 11 '19 13:07 echamudi

@ezhmd Yes and no – as discussed in the linked content: https://exploringjs.com/impatient-js/ch_booleans.html#pitfall-truthiness-checks-are-imprecise

rauschma avatar Jul 11 '19 14:07 rauschma

I can't seem to use the nullish coalescing operator (and optional chaining, too, I think) with ava, as configured in the book's downloaded code. I'm using node 14.15.4, which supports it. I was trying to use it in code from a different chapter and it starts complaining at the second question mark, where it expected an identifier. I can run the module from node directly, and it runs fine (with an additional console.log to make sure the code is being executed).

From what I've been able to piece together, ava is using esm (instead of babel), but esm does not support nullish coalescing or optional chaining. At this point, I'm out of my depth. I don't know if there's a way to tweak the configuration. If not, it might be worth mentioning in case a student tries to use this in one of their solutions.

I'm also a little surprised that my code is being transformed before being executed by node. I'm hoping to learn the language as directly and simply as possible, with the least possible external cruft.

mday64 avatar Jan 30 '21 17:01 mday64

@mday64 Thanks for the feedback!

  • The current setup is indeed not ideal. I had to do this to provide ESM support even on older Node.js versions.
    • The exercises of the next edition of “Impatient JS” won’t be backward-compatible with older Node.js versions and tests will run directly on Node.js.
  • This is a quick fix that should get you to where you want to go: https://gist.github.com/rauschma/f2ee470cb2ff1acfdf205805d3670ae1
    • Warning: not tested as extensively as a real release!

rauschma avatar Jan 30 '21 20:01 rauschma

Hello @rauschma

In 13.6.1 Comma operator and 13.6.2 void operator, I think the links to Speaking JavaScript need to be changed to https://exploringjs.com/es5/ch09.html#comma_operator and https://exploringjs.com/es5/ch09.html#void_operator respectively.

cristigar avatar Nov 15 '22 22:11 cristigar

@rauschma I found while playing around, that the behavior of operator + can be changed by the presence of the valueOf function, even if one value is a string

let str = new String("hello");
str.valueOf = function(){
    return 34;
}
global.valueOf = function(){
    return 5;
}
global.toString = function(){
    return '45';
}
console.log(global + global, str + 10); // 10, 44

Looks like a number is given more prominence to string, however, I could not find anything like this mentioned in the specs, let me know if anyone has more info. Might have been possible I missed something.

yanjankaf avatar Sep 11 '23 17:09 yanjankaf