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 https://yuvi1422.blogspot.com on July 20, 2019.

--

--

--

Loves coding and enjoys learning new technologies. Website: https://.yuvrajpatil.com

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

Recommended from Medium

Node.js API Authentication with JSON Web Tokens

Creating Firefox browser extensions- 12

Asynchronous Programming: Callbacks vs Promises vs Async/Await

My experience with PWA and Electron. Which is better? Which one should I use?

Working with JSON in .NET Core 3

The Ultimate Guide to Creating Auto-Saving Forms in Angular

Plus One Problem

Prepare for Salesforce JavaScript Developer 1 from Scratch

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: https://.yuvrajpatil.com

More from Medium

Javascript Concepts

Basic JavaScript

JavaScript Fundermentals

JavaScript Closure