Things to avoid in JavaScript (the bad parts)
A quick list of things to avoid when writing JavaScript code
- Avoid creating a new object by using
new Object()
. Use the object literal syntax{}
instead. - Same thing for arrays, favor
[]
overnew Array()
. - Avoid blocks except where statements require them (
if
,switch
, loops,try
). - Never assign inside an
if
ofwhile
statements condition part - Never use
==
and!=
. Use===
and!==
instead. - 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. - Never use
with
, as it modifies the scope chain and can be a source of confusion. - Always pass functions to
setTimeout
andsetInterval
- Never use
Array
as an associative arrays, useObject
instead. The part of theArray
object that provides that functionality is in fact provided by theObject
prototype, so you could really have used aDate
object for that same thing. - 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 - Never modify the prototypes of the built-in objects
Object
andArray
. Modify other prototypes of other objects such asFunction
with caution as it could lead to bugs hard to debug.
→ Get my JavaScript Beginner's Handbook
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025