Writing Javascript

When writing javascript is there a format ? I guess the question is what is best practice for writing javascript. Should the const come first followed by function then conditions or loops ?

Check this out, I hope it answers some of your questions:

2 Likes

Ask 20 programmers this question and you’ll get 25 1/2 answers :smile:

Organizational skills will come with time. Generally, I keep variable declarations - including function expressions - at the top of their scope (usually at the top of their function) and function definitions below that. For the most part, their position is more about human readability than anything to do with how the code is executed. Loops, on the other hand, need to be wherever they’ll be used.

As you learn more, you’ll find many more things that will vary with style. Semicolons, or no? Parentheses inside or outside of an IIFE? Tabs or spaces? There are actually plugins for desktop editors that will check the style for you, so you can get an error or warning when you’re deviating from your workplace’s style.

1 Like