Check if a Year is a Leap Year using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jan 26, 2022

banner

Photo from Unsplash

Check if a Year is a Leap Year using JavaScript #

To check if a year is a leap year:

  1. Create a new Date object that stores the 29th of February.
  2. If the date February the 29th exists in the year, then the year is a leap year.
  3. February 29 is a date that occurs every four years, and is called a leap year.
index.js
function isLeapYear(year = new Date().getFullYear()) { return new Date(year, 1, 29).getDate() === 29; } console.log(isLeapYear(2023)); // 👉️ false console.log(isLeapYear(2024)); // 👉️ true

We created a reusable function that takes a year as a parameter and checks if the year is a leap year.

If no parameters are passed to the function, it uses the current year.

The 3 parameters we passed to the Date() constructor are:

  1. year - an integer that represents the year.
  2. monthIndex - a zero-based value that represents the month, where 0 is January, 1 is February, etc.
  3. day - an integer that represents the day of the month.

We created a Date object that stores the date for February 29th.

The getDate() method returns a number between 1 and 31 that represents the day of the month for the given date.

The Date object in JavaScript automatically adjusts the necessary values if passed a greater number.

For example, if you pass 29 as the day of the month for a month that only has 28 days, the Date object would set the date to the first of the next month.

index.js
const date = new Date(2022, 1, 29); // 👇️ Tue Mar 01 2022 console.log(date);

We passed 29 for the day of the month when creating the Date object, but because the month of February only has 28 days in 2022, the Date object created a date for the 1st of March.

February 29 is a date that only occurs every four years, and is called a leap year. If the date exists in the year, then the year is a leap year.

You can also use a more explicit approach.

To check if a year is a leap year:

  1. Check if the year is divisible by 4 and is not an end-of-century year.
  2. Or check if the year is divisible by 400.
  3. If either condition is met, the year is a leap year.
index.js
function isLeapYear(year = new Date().getFullYear()) { return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0; } console.log(isLeapYear(2023)); // 👉️ false console.log(isLeapYear(2400)); // 👉️ true console.log(isLeapYear(2024)); // 👉️ true

For a year to be a leap year, it has to be divisible by 4, except for end-of-century years, which must be divisible by 400.

The modulo (%) operator returns the remainder of the division.

This solution is quite a bit more technical and less readable than the previous one.

Which approach you pick is a matter of personal preference. I'd go with checking if February the 29th exists in the year because I find the solution more direct and readable.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.