Clamping values in an array
When your data can contain “outliers” that are clearly unintentional, you can clamp them to all fall within the same valid range.
Read full articleIf an arrow function immediately returns a value, we don’t need to write the return
keyword. The return is implied if the only statement in the function is a value.
If that value is an object, we need to wrap that object in parentheses for the implicit return
to work.
// the `return` is optional in arrow functions that immediately return a value
const explicitAdd = (a, b) => return a + b
const implicitAdd = (a, b) => a + b
// this does NOT work when the return value is an object
const myFunction = value => {
key: value,
length: value.length
}
// ↑ this does not work
// we have to wrap the object in parentheses in this scenario
const myFunction = value => ({
key: value,
length: value.length
})
// ↑ this is fine
// if anything else happens in the function, like assigning variables or
// calling other functions, we HAVE to be explicit and write the `return`
const myFunction = value => {
const length = value.length
return {
key: value,
length: length
}
}
// the `return` is optional in arrow functions that immediately return a value
const explicitAdd = (a, b) => return a + b
const implicitAdd = (a, b) => a + b
// this does NOT work when the return value is an object
const myFunction = value => {
key: value,
length: value.length
}
// ↑ this does not work
// we have to wrap the object in parentheses in this scenario
const myFunction = value => ({
key: value,
length: value.length
})
// ↑ this is fine
// if anything else happens in the function, like assigning variables or
// calling other functions, we HAVE to be explicit and write the `return`
const myFunction = value => {
const length = value.length
return {
key: value,
length: length
}
}
// the `return` is optional in arrow functions that immediately return a value
const explicitAdd = (a, b) => return a + b
const implicitAdd = (a, b) => a + b
// this does NOT work when the return value is an object
const myFunction = value => {
key: value,
length: value.length
}
// ↑ this does not work
// we have to wrap the object in parentheses in this scenario
const myFunction = value => ({
key: value,
length: value.length
})
// ↑ this is fine
// if anything else happens in the function, like assigning variables or
// calling other functions, we HAVE to be explicit and write the `return`
const myFunction = value => {
const length = value.length
return {
key: value,
length: length
}
}
// the `return` is optional in arrow functions that immediately return a value
const explicitAdd = (a, b) => return a + b
const implicitAdd = (a, b) => a + b
// this does NOT work when the return value is an object
const myFunction = value => {
key: value,
length: value.length
}
// ↑ this does not work
// we have to wrap the object in parentheses in this scenario
const myFunction = value => ({
key: value,
length: value.length
})
// ↑ this is fine
// if anything else happens in the function, like assigning variables or
// calling other functions, we HAVE to be explicit and write the `return`
const myFunction = value => {
const length = value.length
return {
key: value,
length: length
}
}
When your data can contain “outliers” that are clearly unintentional, you can clamp them to all fall within the same valid range.
Read full articleWe commonly increment a number using the += operator. Did you know there are assignment shorthands for other operations as well?
Read full articleWe can skip the return keyword if arrow functions immediately return a value. If that value is an object, we need to also wrap it in parentheses.
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 articleYet another use for the ternary operator: only change the values in an array that match a condition.
Read full articleUsing .replace() on strings only replaces the first occurrence of a substring by default. We can extend that with a “global” flag on the expression.
Read full article