JavaScript Program to Check if a Given Year is Leap Year

To check if a given year is leap year, we have used two approaches in this article. A leap year has 366 days. In every 4th year, an additional day is added to the month of February to synchronize it with the astronomical year.

In this article, we are given with two years to check. Our task is to write a JavaScript program to check if a given year is leap year.

Leap Year Rules

A year is considered a leap year if it follows these rules:

  • Divisible by 4 AND not divisible by 100
  • OR divisible by 400

Approaches to Check Leap Year

Here is a list of approaches to check if a given year is leap year in JavaScript which we will be discussing in this article with stepwise explanation and complete example codes.

Using if-else Statement

In the if-else conditional statement, the if block is executed when the specified condition is TRUE, otherwise, the optional else block is executed.

  • We specify two years which we are going to check.
  • Then we use if/else statement to check if the specified years follow the condition to be a leap year.
  • The conditions are: the given year must be divisible by 4 and not divisible by 100 or must be divisible by 400.

Example

Here is a complete example code implementing above mentioned steps to check if a given year is leap year in JavaScript using if/else statement.

let year1 = 2021;
let year2 = 2024;

function isLeapYear(year) {
    if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)){
        console.log(`${year} is a leap year.`);
    }
    else {
        console.log(`${year} is not a leap year.`);
    }
}

isLeapYear(year1);
isLeapYear(year2);
2021 is not a leap year.
2024 is a leap year.

Using Date Object

In this approach to check if a given year is leap year we use the Date object to determine if February 29th exists in the given year.

  • We declare two variables for checking if the years are leap year or not. We also create a function isLeapYear() which accepts year as parameter.
  • Then we use new Date(year, 1, 29) which attempts to create February 29th of the given year.
  • We use getDate() method to check whether February 29 actually exists in the specified year.
  • If the date is still 29, then it's a leap year. If it rolls over to March 1st, it's not a leap year.

Example

Here is a complete example code implementing above mentioned steps to check if a given year is leap year in JavaScript using Date object.

let year1 = 2021;
let year2 = 2024;

function isLeapYear(year) {
    const date = new Date(year, 1, 29); 
    if (date.getDate() === 29) {
        console.log(`${year} is a leap year.`);
    } else {
        console.log(`${year} is not a leap year.`);
    }
}

isLeapYear(year1); 
isLeapYear(year2); 
2021 is not a leap year.
2024 is a leap year.

Comparison

Method Approach Performance Readability
if-else Statement Mathematical calculation Faster More explicit logic
Date Object Date validation Slightly slower More intuitive

Conclusion

Both methods effectively check for leap years. The if-else approach uses direct mathematical conditions, while the Date object method leverages JavaScript's built-in date handling. Choose based on your preference for explicit logic versus built-in functionality.

Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.
Updated on: 2026-03-15T23:18:59+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements