- Semicolons are required, always.
// BAD
const name = 'Eduardo'
// GOOD
const name = 'Eduardo';
- const and let for variable declaration
// BAD
var name = 'Eduardo';
var lastname = 'Peredo';
lastname = 'Rivero';
// GOOD
const name = 'Eduardo';
let lastname = 'Peredo';
lastname = 'Rivero';
- Prefer map, filter, reduce over forEach as much as you can
// BAD
const values = [1, 2, 3];
let sum = 0;
const greaterThanTwo = [];
const multiplyByTwo = [];
values.forEach(value => {
sum += value;
});
values.forEach(value => {
if (value > 2) {
greaterThanTwo.push(value);
}
});
values.forEach(value => {
multiplyByTwo.push(value * 2);
});
// GOOD
const values = [1, 2, 3];
const sum = values.reduce((acum, value) => (acum + value), 0);
const greaterThanTwo = values.filter(value => value > 2);
const multiplyByTwo = values.map(value => value * 2);
- Use lowerCamelCase notation to write code
// BAD
function GenerateName() {
const MyName = 'A bad name';
return MyName;
}
// GOOD
function generateName() {
const myName = 'Eduardo';
return myName;
}
- Use property shorthand all the time
// BAD
const name = 'Eduardo';
const awesomePerson = {
name: name,
};
// GOOD
const name = 'Eduardo';
const awesomePerson = {
name,
};
- Write object properties in alphabetic order ❗️
// BAD
const awesomePerson = {
weight: 63,
height: 1.69,
age: 31,
name: 'Eduardo',
};
// GOOD
const awesomePerson = {
age: 31,
height: 1.69,
name: 'Eduardo',
weight: 63,
};
- Always use arrow-function for anonymous functions
// BAD
axios.get('/users').then(function(res) {
return res.data;
});
// GOOD
axios.get('/users').then(res => res.data);
- Use trailing comma always
// BAD
const person = {
name: 'Eduardo',
age: 20
};
// GOOD
const person = {
name: 'Eduardo',
age: 20,
};
- Use spread operator for clone/copy objects
// BAD
const myObj = { name: 'Eduardo' };
const newObj = myObj;
// GOOD
const myObj = { name: 'Eduardo' };
const newObj = { ...myObj };
- Prefer ternary operator for short conditionals
// BAD
if (number > 10) {
result = isGreatherThanTen();
} else {
result = isNotGreatherThanTen();
}
// GOOD
result = number > 10 ? isGreatherThanTen() : isNotGreatherThanTen();
- Use normal function definitions for functions inside objects
// BAD
const person = {
name: 'Eduardo',
greeting() {
return `Hello ${this.name}`;
},
};
// GOOD
function greeting() {
return `Hello ${this.name}`;
}
const person = {
name: 'Eduardo',
greeting,
};
- Always define function before they are used
// BAD
const person = {
name: 'Eduardo',
greeting,
};
function greeting() {
return `Hello ${this.name}`;
}
// GOOD
function greeting() {
return `Hello ${this.name}`;
}
const person = {
name: 'Eduardo',
greeting,
};
- Always put use strict at the start of the file (nodejs only)
// BAD
module.exports = someModule;
// GOOD
'use strict';
module.exports = someModule;
- Use object/array destructuring as much as you can
// BAD
const person = {
age: 20,
name: 'Eduardo',
weight: 20,
};
const age = person.age;
const name = person.name;
const weight = person.weight;
// GOOD
const person = {
age: 20,
name: 'Eduardo',
weight: 20,
};
const { name, age, weight } = person;
// BAD
const values = [2, 4, 6];
const two = values[0];
const four = values[1];
const six = values[2];
// GOOD
const values = [2, 4, 6];
const [two, four, six] = values;
- Template literals for concatenate values
// BAD
const name = 'Eduardo';
const lastName = 'P. Rivero';
const fullName = name + lastName;
// GOOD
const name = 'Eduardo';
const lastName = 'P. Rivero';
const fullName = `${name} ${lastname}`;
- Use Number over parseInt for type casting
// BAD
const ageInString = '30';
const ageInNumber = parseInt(ageInString);
// GOOD
const ageInString = '30';
const ageInNumber = Number(ageInString);
- Use === to avoid automatic coersion
// BAD
if (value == value2) {
}
// GOOD
if (value === value2) {
}
❗️ = There are exceptions over this rule