/

Things to Avoid in JavaScript (The Bad Parts)

Things to Avoid in JavaScript (The Bad Parts)

Here is a quick list of things to avoid when writing JavaScript code:

  1. Avoid creating a new object by using new Object(). Instead, use the object literal syntax {}.

  2. Similarly, when creating arrays, favor [] over new Array().

  3. Avoid unnecessary blocks, unless they are required by statements such as if, switch, loops, or try.

  4. Avoid assigning values inside the condition part of if or while statements.

  5. Never use loose equality operators == and !=. Always use strict equality operators === and !== instead.

  6. Avoid using eval. This function has performance issues, as it runs the interpreter/compiler. It also poses security risks, such as code injection when used with user input. Additionally, debugging code that utilizes eval can be challenging.

  7. Do not use with statements, as they modify the scope chain and can cause confusion.

  8. When using setTimeout and setInterval, make sure to always pass functions as arguments.

  9. Avoid using Array as associative arrays. Instead, use Object. The functionality provided by Array for associative arrays is actually provided by the Object prototype. Thus, you can achieve the same result using a Date object.

  10. Do not use \ at the end of a string to create a multiline string, as it is not part of ECMAScript. Instead, use string concatenation 'string1 ' + 'string2'.

  11. Never modify the prototypes of built-in objects such as Object and Array. If you need to modify other prototypes, such as Function, do so with caution, as it can lead to hard-to-debug bugs.

Tags: JavaScript, programming, best practices