Array methods shorthand
We can skip the arrow function when we pass the iterator in filter and map through to a second function.
Read full articleIf an expression returns a Boolean value, comparing that result to true
or false
is redundant. Leave it out to make your code shorter and more readable.
// before: comparing the Boolean result to `true` or `false` is redundant
if (number < 10 === true) { /* code */ }
// after: using the comparison’s result directly is shorter and more readable
if (number < 10) { /* code */ }
// before: the ternary operator is redundant in this assignment as well
const isHello = string === "Hello" ? true : false
// after: the comparison is already `true` or `false`, which we can use directly
const isHello = string === "Hello"
// before: this if-else-block only returns the result of a conditional chain
const isTheWeekend = day => {
if (day === "Saturday" || day === "Sunday") {
return true
} else {
return false
}
}
// after: our function can return the value of the conditional chain directly
const isTheWeekend = day => day === "Saturday" || day === "Sunday"
// before: comparing the Boolean result to `true` or `false` is redundant
if (number < 10 === true) { /* code */ }
// after: using the comparison’s result directly is shorter and more readable
if (number < 10) { /* code */ }
// before: the ternary operator is redundant in this assignment as well
const isHello = string === "Hello" ? true : false
// after: the comparison is already `true` or `false`, which we can use directly
const isHello = string === "Hello"
// before: this if-else-block only returns the result of a conditional chain
const isTheWeekend = day => {
if (day === "Saturday" || day === "Sunday") {
return true
} else {
return false
}
}
// after: our function can return the value of the conditional chain directly
const isTheWeekend = day => day === "Saturday" || day === "Sunday"
// before: comparing the Boolean result to `true` or `false` is redundant
if (number < 10 === true) { /* code */ }
// after: using the comparison’s result directly is shorter and more readable
if (number < 10) { /* code */ }
// before: the ternary operator is redundant in this assignment as well
const isHello = string === "Hello" ? true : false
// after: the comparison is already `true` or `false`, which we can use directly
const isHello = string === "Hello"
// before: this if-else-block only returns the result of a conditional chain
const isTheWeekend = day => {
if (day === "Saturday" || day === "Sunday") {
return true
} else {
return false
}
}
// after: our function can return the value of the conditional chain directly
const isTheWeekend = day => day === "Saturday" || day === "Sunday"
// before: comparing the Boolean result to `true` or `false` is redundant
if (number < 10 === true) { /* code */ }
// after: using the comparison’s result directly is shorter and more readable
if (number < 10) { /* code */ }
// before: the ternary operator is redundant in this assignment as well
const isHello = string === "Hello" ? true : false
// after: the comparison is already `true` or `false`, which we can use directly
const isHello = string === "Hello"
// before: this if-else-block only returns the result of a conditional chain
const isTheWeekend = day => {
if (day === "Saturday" || day === "Sunday") {
return true
} else {
return false
}
}
// after: our function can return the value of the conditional chain directly
const isTheWeekend = day => day === "Saturday" || day === "Sunday"
We can skip the arrow function when we pass the iterator in filter and map through to a second function.
Read full articleWe can split an array in two based on a condition: matches go in the first array, everything else goes in the second.
Read full articleWe can shallowly merge objects without having to type out all properties by hand.
Read full articleAn “XOR” operation returns the elements that only exist in one of two arrays, but not both.
Read full articleWhen a feature calls for the first five photos from a list and a link that says “27 more photos”, we can split the list into these two blocks.
Read full articleInstead of passing a list of parameters to a function, we can pass it an object. That way, we have to explicitly label each parameter we pass to it.
Read full article