Best Practices of JavaScript

  1. Variable Naming

Always try to declare a meaningful name and avoid extra words. Please make sure when other developers read your code, they can understand the naming purpose.

2. Function Naming

Make your function name long and descriptive.

3. Functions Arguments

Avoid too many arguments. If you need, use an object and make it one! use Default's argument instead of using unnecessary conditions.

4. Eval

Don’t use Eval. It poses a huge security risk because it grants far too much power to the passed-in text. Avoid it!

5. Prototype Pollution

Don’t Pollute global Prototype. It’s a very bad practice.

6. Conditional Shorthand

Using Conditional Shorthand makes your code cleaner.

7. Use Curly Braces and Semicolons

Don’t try to omit curly braces and semicolons. Though most browsers will allow you to get away with omitting these, it’s a really bad practice.

8. Comment Your Code

Try to comment on your code as best as possible. It might seem unnecessary but a few weeks when you need to revisit your code or you need to send your code to someone, comments make this more comfortable.

9. Don’t Pass a String to “SetInterval” or “SetTimeOut”

It works the same way as the Eval function would. Instead of string pass a function.

10. Use IIFE

Use IIFE as much as possible. It’s a really good practice.

--

--