11 Things to avoid in JavaScript

To write clean and better performant code in JavaScript, you need to take care of some things. Here I will list 11 things to avoid in JavaScript –

  1. Avoid creating a new object by using new Object(). Use the object literal syntax {} instead. {} is the literal for object in Javascript, and literals are evaluated faster.
  2. Same thing for arrays, favor [] over new Array().
  3. Avoid blocks except where statements require them (ifswitch, loops, try).
  4. Never assign inside an if of while statements condition part
  5. Never use == and !=. Use === and !== instead.
  6. Never use eval. Why? It has performance issues (it runs the interpreter/compiler), it has security issues (code injection if used with user input), difficulties in debugging.
  7. Never use with, as it modifies the scope chain and can be a source of confusion.
  8. Always pass functions to setTimeout and setInterval
  9. Never use Array as an associative arrays, use Object instead. The part of the Array object that provides that functionality is in fact provided by the Object prototype, so you could really have used a Date object for that same thing.
  10. Don’t use \ at the end of a string to create a multiline string, it’s not part of ECMAScript. Use string concatenation ' string1 ' + ' string2 ' instead
  11. Never modify the prototypes of the built-in objects Object and Array. Modify other prototypes of other objects such as Function with caution as it could lead to bugs hard to debug.

References :



Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.