//longhand
if (x === 'orange' || x === 'red' || x === 'gray') {
// do something
}
//shorthand
if (['orange', 'red', 'gray'].includes(x)) {
// do something
}
Loops
//longhand
for (var i = 0; i < productsList.length; i++){
// do things
}
//shorthand (to access the index)
for (let i in productsList){
// do things
}
// or (to access productsList's elements)
for (let i of productsList){
// do things
}
// or with foreach
productsList.forEach(element => {
// do things
});
//longhand
function welcomeMessage(name) {
console.log('Welcome ', name);
}
//shorthand
welcomeMessage = name => console.log(name);
Template literal
// longhand
const winnerMsg = 'Congrats to the winner: ' + winnerName + ', you got a ' + gift;
// shorthand
const winnerMsg = `Congrats to the winner: ${winnerName}, you got a ${gift}`;
Multiple lines
// Longhand
const msg = 'Working in conjunction with humanitarian aid agencies,\n\t'
+ 'we have supported programmes to help alleviate human suffering. \n\t';
//Shorthand
const msg = `Working in conjunction with humanitarian aid agencies,
we have supported programmes to help alleviate human suffering.`;
Null, Undefined, Empty checks
Multiple conditions
Loops
Implicit return
Spread operator
Arrow function
Template literal
Multiple lines
Array.find
https://levelup.gitconnected.com/9-tricks-for-writing-short-and-smart-javascript-code-6c832290e0b1
保持代码好习惯