Jason Knight
2 min readJun 22, 2020

--

Your truthy check is utter nonsense as the two code examples DO NOT DO THE SAME THING! the first one will not trap 0, false, empty string, etc. This is often needed on purpose so that entire section serves no purpose.

Ternary operators don't have to get sloppy, just don't slop them all onto one huffing line. Whitespace, USE IT! A few extra () if really needed for clarity aren't likely to hurt anything either.

The while count-down is NOT something I would recommend because of the 25% performance hit over a for loop, particularly if you store the breakpoint.

for (var i = 0, iLen = array.length; i < iLen; i++) {

Will run circles around "while" all day every day. .. and if you don't actually need the index:

for (var item of array) {

Blows them all away. And if it's an array-like such as a nodelist, where you know all values are truthy...

for (var i = 0, item; itemp = nodeList[i]; i++) {

Is a decent alternative to "for/of" if you need IE 11/earlier support.

Your "after" on ternary should be two separate snippets to make it clearer you're showing two different formats of handling it.

That said, the bitwise not trick is spot on. Since in 32 bit parliance 0xFFFFFFFF is -1. It's actually why -1 is by default the returned value for so many functions and it's surprising how many people don't catch that. I suspect that it's not used as a code clarity issue on the assumption "beginners are too stupid to handle binary". I hate it when people assume beginners are too stupid to understand something.

--

--

Jason Knight
Jason Knight

Written by Jason Knight

Accessibility and Efficiency Consultant, Web Developer, Musician, and just general pain in the arse

No responses yet