Boolean chain

Leap
Leap in JavaScript
export function isLeap(year) {
  return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

The first boolean expression uses the remainder operator to check if the year is evenly divided by 4.

  • If the year is not evenly divisible by 4, then the chain will "short circuit" due to the next operator being a logical AND (&&), and will return false.
  • If the year is evenly divisible by 4, then the logical NOT operator is used to check if the year is not evenly divisible by 100.
  • If the year is not evenly divisible by 100, then the expression is true and the chain will "short-circuit" to return true, since the next operator is a logical OR (||).
  • If the year is evenly divisible by 100, then the expression is false, and the returned value from the chain will be if the year is evenly divisible by 400.
year year % 4 == 0 year % 100 != 0 year % 400 == 0 is leap year
2020 true true not evaluated true
2019 false not evaluated not evaluated false
2000 true false true true
1900 true false false false

The chain of boolean expressions is efficient, as it proceeds from testing the most likely to least likely conditions.

Shortening

By using the falsiness of 0, a test for a value equaling 0 can be shortened using the logical NOT operator, like so

export function isLeap(year) {
  return !(year % 4) && (year % 100 != 0 || !(year % 400));
}

It can be thought of as the expression not having a remainder.

When the body of a function is a single expression, the function can be implemented as an arrow function, like so

export const isLeap = (year) =>
  year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);

or

export const isLeap = (year) =>
  !(year % 4) && (year % 100 != 0 || !(year % 400));

Notice that return and the curly braces are not needed.

15th May 2024 · Found it useful?