Lannie Rose
1 min readAug 31, 2022

--

Good article, Fatfish, and one simple enough for me to understand! To elaborate on them just a bit:

1. Good tip. Speaking of [].includes, use it instead of [].indexOf(item) >= 0 or the more clever ~[].indexOf(item). I guess I got used to the .indexOf forms before .includes was available, or maybe I just wasn't aware of it.

2. Yes I need to start using .every and .some. Just haven't lodged them firmly in my working memory yet!

3. Early return vs. if...else. Yes, but I always worry about which is clearer code. Also, "optimize" in what sense? Whether it is a performance optimization depends on how the javascript engine implements it, no?

4. Yes, but beware! I learned the hard way that the function argument default parameter is NOT equivalent to a first statement in the function of a = a || defaultValue. If the function is passed a value of null, it will remain null even if there is a default parameter, while the statement assigns the defaultValue to a. So the function argument default parameter works only when the parameter is not passed, not when it is passed a falsey value.

5. && instead of if. I use it a lot, but again, I worry about code clarity, in this case using a logical operator as flow control.

--

--

Lannie Rose
Lannie Rose

Written by Lannie Rose

Nice to have a place where my writing can be ignored by millions

Responses (1)