JavaScript Coding Style and Comments (Best Practice)

As a programmer /developer, our code must be as clean and easy to read. We need to write comments to understand the codes for others.

Coding Style

Curly Braces

Bad practice of writing JavaScript code:-

Best practice to use Curly Braces:-

Line Length

Long Horizontal code is bad. It’s very irritating for code readers.

in if statement:

You need to split the long code.

for if statement:

Code Indentation

There are two types of Code Indentation.
1. Horizontal Code Indentation
2. Vertical Code Indentation
Horizontal Indents: 2 or, 4 spaces for horizontal indentation is best practice for writing code.

Vertical Indents: Splitting the code into logical blocks using empty lines
function pow(num, sup) {
let result = 1;

Semicolons

You must need to use a semicolon after each code statement.

Nested Code

Always Try to avoid Nesting code too many levels deep.
You can use the continue or, return keyword to avoid nested code.

use of continue keyword

Avoid nesting with continue keyword:-

use of return keyword

avoid nesting using the return keyword