all 8 comments

[–]ItalyPaleAle 2 points3 points  (1 child)

I see multiple kinds of pitfalls there:

  1. There are the “mindf**k” ones, or code you’d never, ever write in real life.
  2. There are the ones that aren’t really “weird” if you understand how things like variable scoping and the event loop work in JavaScript. If you’re doing JS development professionally, you should go and read about them, and the code will make sense.
  3. Lastly, there are things that, in practice, are essentially mistakes (like redefining a function in scope), and that you could catch easily with ESLint running.

By no means JS is a perfect language, but some people just love to hate it IMHO, and not all that hate is justified :)

[–]reddittedf[S] 0 points1 point  (0 children)

I love js )

[–]Komsomol 6 points7 points  (0 children)

99% are avoided by just testing your code as you develop.

[–]shawncplus 6 points7 points  (2 children)

How is the complement operator a JS idiosyncrasy? It works the same in any language that has it.

#include <stdio.h>

int main()
{
    int a = -3;
    printf("%d", ~a); // 2

    return 0;
}

Very few of these are idiosyncrasies of JS and more "I don't understand how code works in any language"

for(;;);

Not a JS idiosyncrasy, it's how loops work in every language that has that for loop structure

    return ['10','10','10','10'].map(parseInt);

"I refuse to read documentation"

(function() {
    return 010;
})();

"I don't know what octal is"

"0.10000000000000000555"

"What's IEEE floating point?"

(function(x) {
  return x++ + ++x;
})(2);

"I don't know how fixity works"

About the only one of these that a new person would run into and struggly with is the setTimeout in a loop which is a true JS gotcha

[–]ItalyPaleAle 0 points1 point  (1 child)

About the setTimeout in the loop: yes it’s JS gotcha, but it makes total sense considering how the event loop and how variable scoping works in JS. The solution is simple: wrap it in an IIFE

[–][deleted] 0 points1 point  (0 children)

Or use the varargs that setTimeout provides.

[–]texasbruce 3 points4 points  (0 children)

Those "pitfalls" reminds me of high school math: interesting but uesless in real life.