How to write a clean code in JavaScript

Writing Clean Code Makes You More Productive.

Photo by Clément Hélardot on Unsplash

Why Clean Code

Major Coding Conventions for JavaScript

JavaScript style guide

1.Blank Space

・The basic indentation is two spaces. Don’t use tabs.
・Put a space after comma or semicolon.
・Binary operators should be separated by spaces.
・Do not put spaces at the end of lines!
・Use blank lines to separate before/after definition blocks like functions, objects, etc.

2.Coding Style

・The maximum line length should be agreed upon at the team-level. It is usually 80 or 120 characters.

・In inline functions or objects, put a space before and after the curly brackets, except before commas and semicolons.

Always put “else” on a single line.

・Do not use “else” after “return”.

3.Naming Conventions

・For Variable or Function names, use camel case format. First letter should be lowercase.
・Constant names are in all-caps underscore format.
・For Constructor(Class) names, use camel case format. First letter should be uppercase.

・Private members should start with “_”.
・Event Handler Function should start with “on”.


・Make sure there is no duplicates of variable declaration.
・Use [value1, value2] for making JavaScript Array.
・Use { key: value } for making JavaScript Object.
・Do not compare boolean values to true/false.

You want to learn more about readable code?

Google JavaScript Style Guide

・JS File names must be all lowercase.

・Every statement must be terminated with a semicolon.

・String literals are delimited with single quotes ('), rather than double quotes (").

Wrapping Up

I’m a front-end web developer based in Vancouver, studying CICCC, an web and mobile app development course.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store