- Immutable data-manipulations
// Instead of
const data = [ /* */ ];
data.push(100, 200, 300);
// Better to
const data = [ /* */ ];
const res = data.concat([], [ 100, 200, 300 ])
const
variables for every case
- Always initialize
let
/ var
variables with null
/ 0
/ ''
- Don't implement excessive if-clauses
// Instead of
function isLoggedIn(user) {
if (user != null) {
return true;
}
return false;
}
// Just simply do:
function isLoggedIn(user) {
return user != null;
}
- Exceptions first: if you need to handle some exception-cases within your function, do it first:
function getProfile(user) {
if (user != null) {
return null;
}
// do smth
return user;
}
- Prefer function expressions (
const func = () => { /* */ }
)
- Prefer full arrow function
- Immutable data-manipulations
// Instead of
const data = [ /* */ ];
data.push(100, 200, 300);
// It's better to
const data = [ /* */ ];
const res = data.concat([], [ 100, 200, 300 ])
- Pay attantion to array entries type:
[ 1, 5, 6, 2, 9 ].sort((a, b) => a - b)
- Prefer
map
, sort
, filter
, some
, every
, forEach
, reduce
- Prefer immutable object-manipulations:
// Instead of
const a = { b: 3 };
a.b = 200;
// It's better to
const c = Object.assign({}, a, { d: 3 });