Logging as objects
This neat little trick makes values logged to the console much more readable with a minor adjustment to how we log it.
Read full articleThe constructor for Boolean values is all you need to filter all falsy values from an array. Keep in mind that this takes out false
, 0
, null
, undefined
, empty strings, and NaN
. If you don’t want that, filter by something more specific.
const values = [
5, null, false, "hi", 0, undefined, { name: "Tim" }, "", true, NaN, [7]
]
// `Boolean` takes out ALL falsy values
values.filter(Boolean)
// ⇒ [5, "hi", { name: "Tim" }, true, [7]]
// `x != null` leaves most falsy values, taking out `null` and `undefined`
values.filter(value => value != null)
// ⇒ [5, false, "hi", 0, { name: "Tim" }, "", true, NaN, [7]]
const values = [
5, null, false, "hi", 0, undefined, { name: "Tim" }, "", true, NaN, [7]
]
// `Boolean` takes out ALL falsy values
values.filter(Boolean)
// ⇒ [5, "hi", { name: "Tim" }, true, [7]]
// `x != null` leaves most falsy values, taking out `null` and `undefined`
values.filter(value => value != null)
// ⇒ [5, false, "hi", 0, { name: "Tim" }, "", true, NaN, [7]]
const values = [
5, null, false, "hi", 0, undefined, { name: "Tim" }, "", true, NaN, [7]
]
// `Boolean` takes out ALL falsy values
values.filter(Boolean)
// ⇒ [5, "hi", { name: "Tim" }, true, [7]]
// `x != null` leaves most falsy values, taking out `null` and `undefined`
values.filter(value => value != null)
// ⇒ [5, false, "hi", 0, { name: "Tim" }, "", true, NaN, [7]]
const values = [
5, null, false, "hi", 0, undefined, { name: "Tim" }, "", true, NaN, [7]
]
// `Boolean` takes out ALL falsy values
values.filter(Boolean)
// ⇒ [5, "hi", { name: "Tim" }, true, [7]]
// `x != null` leaves most falsy values, taking out `null` and `undefined`
values.filter(value => value != null)
// ⇒ [5, false, "hi", 0, { name: "Tim" }, "", true, NaN, [7]]
This neat little trick makes values logged to the console much more readable with a minor adjustment to how we log it.
Read full articleIf an expression returns a Boolean value, we don’t also need to compare that result to true or false.
Read full articleTo filter all falsy values from arrays, all we need to do is pass the Boolean constructor to the filter function.
Read full articleWe can turn arrays of absolute numbers into relative numbers. The largest value becomes 1, with the others calculated based on that largest value.
Read full articleWhen is June the fifth month of the year? When JavaScript is involved, of course. In JavaScript, January is the zero-th month.
Read full articleWhen naming variables that hold Boolean values, using a prefix that indicates as much helps with readability.
Read full article