Getting the largest number from an array
To find the largest value in an array of numbers, we can spread that array into Math.max() instead of manually iterating over it.
Read full articleThe ternary operator can help us reduce duplication. We can even place it inside of a function call if we’d switch the parameters based on a condition.
Watch out for readability: the shortest version isn’t always the best option.
// we want to wish people a happy weekend/workweek depending on the day
const isWeekend = day === "Saturday" || day === "Sunday"
// we can call `console.log` with the respective message like this
isWeekend ? console.log("Happy weekend!") : console.log("Happy workweek!")
// `console.log` happens in any case, so we can move the ternary inside it
console.log(isWeekend ? "Happy weekend!" : "Happy workweek!")
// we can extract the duplicate “Happy ” and “!” to only write them once
console.log(`Happy ${isWeekend ? "weekend" : "workweek"}!`)
// don’t go crazy and extract EVERY duplication; the code becomes shorter
// if we extract the duplicate “w”, but also much harder to read
console.log(`Happy w${isWeekend ? "eekend" : "orkweek"}!`)
// we want to wish people a happy weekend/workweek depending on the day
const isWeekend = day === "Saturday" || day === "Sunday"
// we can call `console.log` with the respective message like this
isWeekend ? console.log("Happy weekend!") : console.log("Happy workweek!")
// `console.log` happens in any case, so we can move the ternary inside it
console.log(isWeekend ? "Happy weekend!" : "Happy workweek!")
// we can extract the duplicate “Happy ” and “!” to only write them once
console.log(`Happy ${isWeekend ? "weekend" : "workweek"}!`)
// don’t go crazy and extract EVERY duplication; the code becomes shorter
// if we extract the duplicate “w”, but also much harder to read
console.log(`Happy w${isWeekend ? "eekend" : "orkweek"}!`)
// we want to wish people a happy weekend/workweek depending on the day
const isWeekend = day === "Saturday" || day === "Sunday"
// we can call `console.log` with the respective message like this
isWeekend ? console.log("Happy weekend!") : console.log("Happy workweek!")
// `console.log` happens in any case, so we can move the ternary inside it
console.log(isWeekend ? "Happy weekend!" : "Happy workweek!")
// we can extract the duplicate “Happy ” and “!” to only write them once
console.log(`Happy ${isWeekend ? "weekend" : "workweek"}!`)
// don’t go crazy and extract EVERY duplication; the code becomes shorter
// if we extract the duplicate “w”, but also much harder to read
console.log(`Happy w${isWeekend ? "eekend" : "orkweek"}!`)
// we want to wish people a happy weekend/workweek depending on the day
const isWeekend = day === "Saturday" || day === "Sunday"
// we can call `console.log` with the respective message like this
isWeekend ? console.log("Happy weekend!") : console.log("Happy workweek!")
// `console.log` happens in any case, so we can move the ternary inside it
console.log(isWeekend ? "Happy weekend!" : "Happy workweek!")
// we can extract the duplicate “Happy ” and “!” to only write them once
console.log(`Happy ${isWeekend ? "weekend" : "workweek"}!`)
// don’t go crazy and extract EVERY duplication; the code becomes shorter
// if we extract the duplicate “w”, but also much harder to read
console.log(`Happy w${isWeekend ? "eekend" : "orkweek"}!`)
To find the largest value in an array of numbers, we can spread that array into Math.max() instead of manually iterating over it.
Read full articleWe can pass an array to JavaScript’s sort function to sort an array of objects by one of their properties.
Read full articleTo asynchronously fetch data in a React component using hooks, we can define and then call an asynchronous function inside of useEffect.
Read full articleWe can create a helper function that makes functions return their Boolean opposite. This can be useful in the shorthand syntax for array methods.
Read full articleAn “XOR” operation returns the elements that only exist in one of two arrays, but not both.
Read full articleWe can use the value returned by Math.random() to get a random element from an array.
Read full article