5 good practices when coding JavaScript

Joaquin Correa
3 min readJun 2, 2021

At first when I began as a web developer student, with limited programming experience, I was having some bad behaviors when doing JavaScript. Fortunately, I was able to correct them with some guidance, and I think it is a good idea to share these good practices for beginners who have started not too long ago. The earlier you get familiar with these habits, the better:

1. Use good names for your variables and functions. Although it may seem trivial at first glance, it is a good practice to apply good descriptive names to your variables and your functions rather than random names. This practice helps to make your code more legible for yourself and others who read it.

Rather than doing this:

let X = setTimeOut(()=>{alert('Time is up')}, 10000);

Do this:

const EXPIRATION_TIME = 10000;let sessionTime = setTimeOut( ()=>{alert('Time is up')}, EXPIRATION_TIME );

2. Use the higher order functions to work with arrays. Functions such as .map(), .filter() and .reduce() were created so that you do not have to reinvent the wheel every time you work on arrays. It is then a good practice to get familiar with them to know when to use them in your code, especially if you have a background with other programming languages.

Instead of doing this:

const agesOfTheTeam = [26, 26, 35, 44, 30, 32, 19];let overForty = [];for(i=0; i<agesOfTheTeam.length; i++){
const memberAge = agesOfTheTeam[i]
if (memberAge > 40){
overForty[..., memberAge]
}
}

JavaScript allows you to express it in an easier way to read it:

const agesOfTheTeam = [26, 26, 35, 44, 30, 32, 19];let overForty = agesOfTheTeam.filter(memberAge => memberAge > 40)

3. Get used to ternaries. They are easy to understand and can result more efficient than doing if else statements for just one case

Rather than coding this:

let message;
if(age){
message = "Come in"
}
else{
message = "Go back to your Nintendo"
}

Code this way:

let message = age ? "Come in" : "Go back to your Nintendo"

4. Make clear comments when necessary. You do not have to add comments to every single part of your code. Only for the parts you feel may be complex to understand. A good rule of thumb to follow when commenting sections of your code is to add comments that not only describe what they are doing (because that would look redundant to the readers) but also the reason why they are doing it.

Avoid these type of comments:

//This function adds a new puppy to the list
addNewPuppy(puppy, puppyList)
Photo by Tom Slatin on www.tomslatin.com

4. Make it pretty. It is a good practice for code to have consistent style for the readers. Although this is not crucial if you do personal solo projects, when it comes to work on a team project for a company, bad formatted code can lead to unnecessary discussions and waste of time. This is why there is an excellent extension for VSCode that takes care of the styling for any project and it’s easy to configure: Prettier.

--

--