JavaScript Best Practices

JavaScript Best Practices

Getting things done is good but getting things done in better way is what matters most. Everyone wants to make their website to be faster, lighter. No one likes slow website. In order to make our website faster we have to set some best practices standards and every teammate should follow that.

I have written down few of the best practices which I recommend to be followed in any JavaScript development.

Category: Performance

  1. VanillaJS is always quicker than using a library. Use VanillaJS as possible if you want highest performance.
  2. Minimize DOM manipulation since it is a performance consuming task.
  3. Minimize number of HTTP calls. Try to use cached HTTP response.
  4. Use lazy loading of resources as much as feasible.
  5. Avoid broadcasting and listening the events.
  6. Avoid adding global variables and global methods.
  7. Use {} instead of new Object().
  8. Use [] instead of new Array().
  9. Declare variables outside of loop.
  10. Place scripts at the bottom of your web page.
  • It will help in speeding up the web page loading.

Category: Risks

  1. eval = Evil

With great power, comes great responsibility.

Eval is very powerful feature of JavaScript, hence it requires to be used very carefully. It can produce an enormous security risk.

2. Use === instead of ==

  • It makes sure that comparison is type safe.

3. Whenever we create any variable, we should try to make it as private as possible. It will help us to avoid name collision with global variables and will provide encapsulation.

4. Use editor plug-in for JS Hint. It will highlight many errors and warnings which will help us to avoid bugs.

5. Add null/undefined checks wherever possible.

Category: Organised Codebase

  1. One file = One Responsibility.
  2. Don’t use short-hand. Even though there is only one statement in a condition block or loop block, it should be enclosed by curly braces.

3. Use configuration instead of hard coding.

4. For defining strings, use single quotes instead of double quotes.

5. Add comments on method level and conditions (if conditions are complex).

6. Always use semicolons to end the statement.

7. Separate file names with dots and dashes.

8. Remove unused and commented code. Remove unused libraries and their references.

Originally published at on July 20, 2019.




Loves coding and enjoys learning new technologies. Website:

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How Node Js and Web works together..

Variables in JavaScript

Angular 13: Noteworthy Add-ons, Enhancements, and Modifications!

Realtime chat app with laravel 5.4 and vuejs 2

Building angular app using browserify

It’s Time To Learn Intermediate JavaScript

Introducing StateLake: You’ll Love Managing States This Way

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
Yuvraj Patil

Yuvraj Patil

Loves coding and enjoys learning new technologies. Website:

More from Medium

Phase-1 JavaScript Project

Asynchronous JavaScript

S.O.L.I.D and JavaScript

Javascript in Leetcode 19–21 | 面試問題