50 Javascript Best Practice Rules to Write Better Code

Always “use strict” On

Use Function expressions instead of Function Declarations

Stop using “var”!

Use “const” and immutability as much as possible

Prefer Pure Functions

Prefer Class over Constructor Functions

Use “destructuring”

Only work with data you need

Always use “===”

Avoid Global Variables

Wrap loose declarations in blocks

Organize your declarations

Don't initialize things with “undefined”

Always initialize your declarations

Lint your code and have a consistent style

Use Typescript

Functions and methods should do one thing only

Don’t be lazy when naming things

Avoid unnecessary declarations

Use default values when posible

Always have a default case for switch statements

Never use “eval”

Avoid the “new” keyword

Add meaningful comments for nonobvious things

Keep ternaries simple

Simplify with optional chaining

Prefer promises over callbacks

For loops > .forEach sometimes

“for…in” and “for…of”

Optimize for loops?

Always “try…catch” JSON methods

Prefer template strings

Avoid nesting or chaining loops

Avoid Weird Unreadable hacks

Prefer the “rest” operator over “arguments”

Prefer “globalThis” for global access

Understand Javascript but Build with Libraries and Frameworks

Add semicolons, always!

Readable > Performance unless you need Performance

Be careful with “Truthy” and “Falsy” checks

Prefer Ternary over logical “||” and “&&” checks

Watch out for “undefined” and “null” with the “??” operator

Be careful with automatic type conversions

Never trust data you don't create

Use regex when extracting and looking for things in Strings

IIFE and small utility libraries

Avoid repeating yourself with utilities

Don’t take advantage of weird Javascript “features”

Add Unit Tests

Blog & YouTube Channel for Web, UI & Software Development - beforesemicolon.comyoutube.com/c/BeforeSemicolon

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