- Siempre agregar punto y coma ; al final de cada statement.
# MAL
const name = 'Eduardo'
// BIEN
const name = 'Eduardo';
- Usar const y let para declarar variables
// MAL
var name = 'Eduardo';
var lastname = 'Peredo';
lastname = 'Rivero';
// BIEN
const name = 'Eduardo';
let lastname = 'Peredo';
lastname = 'Rivero';
- Preferir el uso de map, filter, reduce sobre forEach siempre que sea posible
// MAL
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);
});
// BIEN
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);
- Usar lowerCamelCase en la mayoría de casos para escribir código
// MAL
function GenerateName() {
const MyName = 'A bad name';
return MyName;
}
// BIEN
function generateName() {
const myName = 'Eduardo';
return myName;
}
- Si el nombre de la propiedad y variable son iguales se debe usar la sintaxis corta.
// MAL
const name = 'Eduardo';
const awesomePerson = {
name: name,
};
// BIEN
const name = 'Eduardo';
const awesomePerson = {
name,
};
- Escribir nombre de propiedades en orden alfabético ❗️
// MAL
const awesomePerson = {
weight: 63,
height: 1.69,
age: 31,
name: 'Eduardo',
};
// BIEN
const awesomePerson = {
age: 31,
height: 1.69,
name: 'Eduardo',
weight: 63,
};
- Usar arrow functions para funciones anónimas
// MAL
axios.get('/users').then(function(res) {
return res.data;
});
// BIEN
axios.get('/users').then(res => res.data);
- Poner siempre una coma al final de cada propiedad
// MAL
const person = {
name: 'Eduardo',
age: 20
};
// BIEN
const person = {
name: 'Eduardo',
age: 20,
};
- Usar spread operator para clonar/copiar objetos
// MAL
const myObj = { name: 'Eduardo' };
const newObj = myObj;
// BIEN
const myObj = { name: 'Eduardo' };
const newObj = { ...myObj };
- Preferir uso de ternary operator para condicionales de una linea/declaración
// MAL
if (number > 10) {
result = isGreatherThanTen();
} else {
result = isNotGreatherThanTen();
}
// BIEN
result = number > 10 ? isGreatherThanTen() : isNotGreatherThanTen();
- Definir funciones fuera de los objetos
// MAL
const person = {
name: 'Eduardo',
greeting() {
return `Hello ${this.name}`;
},
};
// BIEN
function greeting() {
return `Hello ${this.name}`;
}
const person = {
name: 'Eduardo',
greeting,
};
- Definir las funciones antes de ser usadas
// MAL
const person = {
name: 'Eduardo',
greeting,
};
function greeting() {
return `Hello ${this.name}`;
}
// BIEN
function greeting() {
return `Hello ${this.name}`;
}
const person = {
name: 'Eduardo',
greeting,
};
- Siempre usar use strict al inicio de cada archivo (solo en entorno de nodejs)
// MAL
module.exports = someModule;
// BIEN
'use strict';
module.exports = someModule;
- Usar object/array destructuring siempre que sea posible
// MAL
const person = {
age: 20,
name: 'Eduardo',
weight: 20,
};
const age = person.age;
const name = person.name;
const weight = person.weight;
// BIEN
const person = {
age: 20,
name: 'Eduardo',
weight: 20,
};
const { name, age, weight } = person;
// MAL
const values = [2, 4, 6];
const two = values[0];
const four = values[1];
const six = values[2];
// BIEN
const values = [2, 4, 6];
const [two, four, six] = values;
- Usar Template literals para concatenar cadenas
// MAL
const name = 'Eduardo';
const lastName = 'P. Rivero';
const fullName = name + lastName;
// BIEN
const name = 'Eduardo';
const lastName = 'P. Rivero';
const fullName = `${name} ${lastname}`;
- Usar Number sobre parseInt para convertir valores a número
// MAL
const ageInString = '30';
const ageInNumber = parseInt(ageInString);
// BIEN
const ageInString = '30';
const ageInNumber = Number(ageInString);
- Usar === para evitar automatic coersion
// MAL
if (value == value2) {
}
// BIEN
if (value === value2) {
}
❗️ = Hay excepciones sobre esta regla