JavaScript: Check if a Year is a Leap Year (2 Ways)

Updated: August 5, 2023 By: Khue Post a comment

Leap years are years with an extra day, February 29, to keep the calendar year synchronized with Earth’s orbit around the sun. They occur every four years, except for years divisible by 100 unless they are divisible by 400. Some examples of leap years are 2020, 2016, 2008, etc.

This succinct, example-based article will walk you through a couple of different ways to check whether a given year is a leap year or not in modern JavaScript (ES6 and beyond). No more delay, let’s get straight to the point.

Using arithmetic operators

In mathematical parlance, a year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. With that in mind, we can take the following steps:

  1. Define a function that takes a year as a parameter.
  2. Inside the function, use the modulo operator (%) to check if the year is divisible by 4, 100, and 400.
  3. Use the logical operators && (and) and || (or) to combine the results of the modulo operations according to the leap year rules.
  4. Return a boolean value indicating if it is a leap year or not.

Example:

// define a function to check leap year
const isLeapYear = (year) => {
  // check if the year is divisible by 4, 100, and 400
  let divisibleBy4 = year % 4 == 0;
  let divisibleBy100 = year % 100 == 0;
  let divisibleBy400 = year % 400 == 0;

  // apply the leap year rules
  return divisibleBy4 && (!divisibleBy100 || divisibleBy400);
};

// test some examples
console.log(isLeapYear(2000)); // true
console.log(isLeapYear(2023)); // false
console.log(isLeapYear(2020)); // true
console.log(isLeapYear(2024)); // true
console.log(isLeapYear(2100)); // false

This approach is simple, concise, and easy to understand. It follows the standard definition of leap years in the Gregorian calendar.

Using Date objects

The core idea here is to determine whether the given year has the day of February 29 or not. This approach is elegant and looks smart. It does not require any arithmetic operations or logical expressions. However, it may not work for dates before 1582, when the Gregorian calendar was introduced. It may also be affected by time zones and daylight-saving time adjustments.

The steps are:

  1. Define a function that takes a year as a parameter.
  2. Inside the function, create a new Date object with the given year, but with a fixed month of February (1) and a fixed date of 29.
  3. Use the getDate() method to get the day of the month from the date object.
  4. Check if the day of the month is equal to 29. If yes, then it is a leap year. If not, then it is not a leap year.

Example:

// define a function to check leap year
function isLeapYear(year) {
  // create a new date object with February 29th
  let date = new Date(year, 1, 29);

  // get the day of the month from the date object
  let day = date.getDate();

  // check if the day is equal to 29
  return day == 29;
}

// test some examples
console.log(isLeapYear(2000)); // true
console.log(isLeapYear(1900)); // false
console.log(isLeapYear(2020)); // true
console.log(isLeapYear(2023)); // false
console.log(isLeapYear(2024)); // true
console.log(isLeapYear(2400)); // true

Conclusion

You’ve learned two techniques to determine if a given year is a leap year. Neither of these techniques relies on a third-party library, and that’s great. This tutorial ends here. If you find something outdated or incorrect in the code examples, please inform me by leaving a comment. Happy coding & have a nice day!