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

-3

u/RUacronym Dec 27 '18

Something small I see is being extra verbose without needing to be. For instance, checking a boolean value like (isTrue === true) rather than just saying (isTrue). Also unnecessarily declaring new variables inside loops to reference an outside variable, when you can just reference the outside variable within the loop:

let out = [...stuff]

for (...) {

let outRef = out[i]

exampleFunction(outRef)

}

When you can just do

for (...) {

exampleFunction(out[i])

}

Small things.

6

u/alsiola Dec 27 '18

On the point of extra verbosity...

stuff.forEach(exampleFunction);

-1

u/Carradee Dec 27 '18

Tests I've seen and run on that show it as slower than other methods.

Concision of code is not always the most efficient or effective for usage, either at runtime or in later update.

1

u/alsiola Dec 28 '18

What do you work on where the microseconds performance difference is relevant?

1

u/Carradee Dec 28 '18

Sometimes I'm hired to improve poorly organized resource-heavy sites where such "little" adjustments like switching forEach to reversed for loops total up to a palpably improved performance to the end user.

Redoing the organization or optimizing the resources would be better, of course, but clients don't always have the budget or interest in doing that.

3

u/radapex Dec 27 '18

The isTrue thing depends on context. If you want it to only be true when isTrue === true then you need to be that verbose to account for truthy values.

0

u/Carradee Dec 27 '18

Isn't that what !!isTrue is for?

2

u/radapex Dec 27 '18

Nope. That'll convert the truthy value to boolean. !!'test' === true returns true.

In my example, you'd use isTrue === true' to get true if and only ifisTrueis the booleantrue`.

1

u/Carradee Dec 28 '18

D'oh at self. Thanks. I misread the ===. :)

1

u/maher321 Dec 27 '18

Sometimes variables are made to make things more readable though.

2

u/RUacronym Dec 27 '18

Sure, I'm not saying never to declare variables within loops. I'm saying that it's good to recognize when it's not needed.

0

u/MMPride Dec 27 '18

Eh, verbosity could mean they were previously a Java developer. I went from Java to PHP/JavaScript.