5 Best Practices for Writing Clean JavaScript

How to make your code self-documenting

Ferenc Almasi
7 min readMar 18, 2020
cleaning codebase
Photo by Verne Ho on Unsplash

When writing our apps, we often tend to overlook the simplest principle when it comes to coding: maintaining a clean and readable source code. You probably heard the phrase of spaghetti code before. Meaning the codebase is hard to maintain, read, or even decipher. Even though the application could work just fine, adding new requirements and keeping support can be equal to hell. And if it comes to bug fixing, time spent searching through the code will grow exponentially.

In this article, I’ve collected five best practices for keeping your JavaScript code as clean as possible. Some of them may sound common sense, but if we do not pay close attention, we may as well forget to use them. So let’s start with some core principles.

#1: Principles

So what is clean code anyway? The answer may differ from person to person but for me, clean code is self-documenting. The intent is clear from first sight, different responsibilities are well separated. You can almost read code like natural language and this means you don’t have to use comments as a crutch. Always favor code instead.

There are some common coding principles which can help us achieve that:

KISS

--

--

Ferenc Almasi

💻 Frontend dev • ✍️ Creator of webtips.dev • 🐦 Tweets at @WebtipsHQ