DEV Community

Farah Ismah Hana
Farah Ismah Hana

Posted on

JavaScript Best Practices: Writing Clean and Maintainable Code

Introduction

When learning JavaScript, we usually focus on making our code work.

But as we write more code, it is also important to think about how easy the code is to read and understand.

Code that is easy to read is easier to update and fix later. This becomes even more important when working on a larger project or working with other developers.

There are many ways to improve JavaScript code, but we do not need to learn everything at once.

In this article, we will look at some simple JavaScript best practices that can help us write code that is clean, easy to read, and easier to maintain.

1. Use const and let

JavaScript provides var, let, and const for creating variables.

For modern JavaScript, we can use const when the value does not need to change.

const name = 'John';
const age = 25;
Enter fullscreen mode Exit fullscreen mode

If the value needs to change later, we can use let.

let score = 80;

score = 90;
Enter fullscreen mode Exit fullscreen mode

A simple rule to remember is:

  • Use const when the value does not change.
  • Use let when the value can change.
  • Avoid using var when writing new code.

Using const and let also makes it easier to understand whether a value is expected to change.

2. Use Clear Variable Names

Variable names should help us understand what the value represents.

For example:

const x = 25;
Enter fullscreen mode Exit fullscreen mode

We cannot easily tell what x means.

A clearer name would be:

const employeeAge = 25;
Enter fullscreen mode Exit fullscreen mode

Another example:

const d = 30;
Enter fullscreen mode Exit fullscreen mode

We can make it clearer:

const daysInMonth = 30;
Enter fullscreen mode Exit fullscreen mode

We do not need to use very long names. The important thing is that the name gives enough information about the value.

Clear variable names make the code easier to read, especially when we come back to the code later.

3. Use === When Comparing Values

JavaScript provides == and === for comparing values.

When possible, it is better to use ===.

const age = 25;

if (age === 25) {
  console.log('Age is 25');
}
Enter fullscreen mode Exit fullscreen mode

The difference can be seen here:

console.log(5 == '5');   // true
console.log(5 === '5');  // false
Enter fullscreen mode Exit fullscreen mode

With ==, JavaScript can convert the value before comparing it.

With ===, JavaScript checks both the value and the type.

For example, 5 is a number, while '5' is a string.

Using === can make comparisons easier to understand.

4. Avoid Unnecessary Complexity

Sometimes we can write the same logic in a simpler way.

For example:

if (user) {
  if (user.isActive) {
    console.log('User is active');
  }
}
Enter fullscreen mode Exit fullscreen mode

We can write it as:

if (user && user.isActive) {
  console.log('User is active');
}
Enter fullscreen mode Exit fullscreen mode

The second example is simpler because we do not need two levels of if.

However, this does not mean that every nested if is bad. If nesting makes the logic easier to understand, it is fine to use it.

The main idea is to avoid adding complexity when we do not need it.

5. Avoid Repeating the Same Code

Sometimes we may write the same code several times.

For example:

console.log('Welcome, John');
console.log('Welcome, Jane');
console.log('Welcome, Michael');
Enter fullscreen mode Exit fullscreen mode

If we have many users, writing the same code again and again is not practical.

We can use an array and a loop:

const users = ['John', 'Jane', 'Michael'];

users.forEach((user) => {
  console.log(`Welcome, ${user}`);
});
Enter fullscreen mode Exit fullscreen mode

Now the same logic can be used for every user.

This also makes the code easier to update later.

6. Keep Your Code Easy to Read

When writing code, we do not always need to make it as short as possible.

For example:

const result = data.filter(x => x.active).map(x => x.name);
Enter fullscreen mode Exit fullscreen mode

This code is valid, but separating the steps can sometimes make it easier to understand.

const activeUsers = data.filter((user) => user.active);

const userNames = activeUsers.map((user) => user.name);
Enter fullscreen mode Exit fullscreen mode

Now we can clearly see what each step does.

Clean code is not necessarily code with fewer lines.

The goal is to make the code easy to read and understand.

Key Takeaways

  • Use const when a value does not need to change.
  • Use let when a value needs to change.
  • Use clear and meaningful variable names.
  • Prefer === when comparing values.
  • Avoid unnecessary complexity.
  • Avoid repeating the same code.
  • Write code that is easy to read and understand.

Conclusion

Writing JavaScript is not only about making the code work. We also need to think about how the code will look when we or someone else reads it later.

Simple habits such as using clear variable names, choosing the right type of variable, and keeping the code simple can make a big difference.

We do not need to follow every best practice from the beginning. As we learn more JavaScript, we can slowly build better habits and improve the way we write code.

Top comments (0)