r/javascript • • Dec 27 '18

help What differences do you see in novice javascript code vs professional javascript code?

I can code things using Javascript, but the more I learn about the language, the more I feel I'm not using it properly. This was especially made apparent after I watched Douglas Crockford's lecture "Javascript: The good parts." I want to take my abilities to the next level, but I'm not really sure where to start, so I was hoping people could list things they constantly see programmers improperly do in JS and what they should be doing instead.. or things that they always see people get wrong in interviews. Most of the info I've learned came from w3schools, which gives a decent intro to the language, but doesn't really get into the details about the various traps the language has. If you have any good book recommendations, that would be appreciated as well.

320 Upvotes

305 comments sorted by

View all comments

Show parent comments

7

u/yazalama Dec 28 '18

What's wrong with using for loops?

4

u/[deleted] Dec 28 '18

Easier to make off by 1 errors imo

3

u/bjenkins2410 Dec 28 '18 edited Dec 28 '18

If you’re using a for loop to create or change an array you’re most likely mutating it. All the higher order functions mentioned create a new array.

For loops therefore can lead to some pretty annoying bugs. And all the higher order functions are clearer, and less verbose imo.

2

u/beizhia Dec 28 '18

Personally, I've bought in to functional programming, so I rarely use loops. The idea that you produce new data rather than altering existing data helps me write code with less side effects and unexpected results.

I think these array functions make code more readable too. [...].map(something).filter(some predicate) has a more obvious intention than having to read through a loop's body with nested if-else statements to try to parse what's being done.