7 Best Practices to Write Better JavaScript
By: Mehdi Aoussiad
Tags: Javascript Tips, JavaScript, Web Development, Programming, Coding

7 Best Practices to Write Better JavaScript

Photo by Nicole Wolf on Unsplash

Introduction

JavaScript is a powerful programming language that allows you to use different approaches when writing code. The developers who master JavaScript and write organized and performant code have become the most sought after in the job market.

In this article, we will share with you some useful JavaScript best practices that you should know. Let’s get right into it.

Image created with ❤️️ By author.

1. Avoid using global variables

Try to minimize the use of global variables as much as you can. This includes all data types, objects, and functions. Because global variables are accessible everywhere and they can be overwritten by other scripts.

Always use local variables instead, and I would also recommend learning about closures if you don’t know them.

2. Use strict mode

Strict mode is declared by adding “use strict” to the beginning of a script or a function. With strict mode, you can not, for example, use undeclared variables.

Here is an example:

"use strict";

myFunction();



function myFunction() {

y = 3.14; // This will also cause an error because y is not declared

}

As you can see, the strict mode will make sure you get errors that would happen silently.

3. Avoid using the var keyword

If you already know about hoisting, you will also know that all the declarations with the keyword var are hoisted, which makes var declarations be accessible before where the declaration happened. That’s why I think using the keywords let and const can be a good practice.

4. Always initialize your variables

It is a good coding practice to initialize variables when you declare them. Because this will give a cleaner code, provide a single place to initialize variables, and avoid undefined values.

// Declare and initiate at the beginning

const firstName = "John",

const lastName = "Doe",

5. Always use the operator " === "

The === operator checks and forces a comparison of values and type. That’s something that most of the time you will do. So, I think making it a habit is not a bad practice because it helps to avoid undesirable effects.

1 === "1" // false

1 === 1 // true

"1" === "1" // true

6. Use destructuring

The destructuring assignment is a special syntax that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

Here is an example:

// With object.

const { name, age } = user;

//Prints: name = 'John Doe', age = 34 // With an array.

const [a, b] = [1, 2, 3, 4, 5, 6];

console.log(a, b); // 1, 2

As you can see, by using the ES6 destructuring, our code becomes cleaner and easier to maintain by other developers. You can check my article below if you are interested.

7. Use TypeScript

TypeScript can help you a lot in delivering better code. It will need some getting used to if you never tried a type system before. But it will become really helpful in the long run because it’s not something complicated, you just need to get into it.

Conclusion

As you can see, I just gave you some simple best practices that I know in JavaScript. By applying these simple rules, your code can be much better.

Thank you for reading this article, I hope you found it useful.

More Reading