JavaScript is powerful and flexible which allows you to code any way you like and try some very unusual things which can lead to a bug in your code. Here are 50 things I learned about coding in JavaScript you should know about.
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 possible
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”
#coding #javascript #best-practices