JavaScript Program to Check Leap Year

To understand this example, you should have the knowledge of the following JavaScript programming topics:


A year is a leap year if the following conditions are satisfied:

  1. The year is a multiple of 400.
  2. The year is a multiple of 4 and not a multiple of 100.

Example 1: Check Leap Year Using if...else

// program to check leap year
function checkLeapYear(year) {

    //three conditions to find out the leap year
    if ((0 == year % 4) && (0 != year % 100) || (0 == year % 400)) {
        console.log(year + ' is a leap year');
    } else {
        console.log(year + ' is not a leap year');
    }
}

// take input
const year = prompt('Enter a year:');

checkLeapYear(year);

Output

Enter a year: 2000
2000 is a leap year

In the above program, the three conditions are checked to determine if the year is a leap year or not.

The % operator returns the remainder of the division.


Example 2: Check Leap Year Using newDate()

// program to check leap year
function checkLeapYear(year) {

    const leap = new Date(year, 1, 29).getDate() === 29;
    if (leap) {
        console.log(year + ' is a leap year');
    } else {
        console.log(year + ' is not a leap year');
    }
}

// take input
const year = prompt('Enter a year:');

checkLeapYear(year);

Output

Enter a year: 2000
2000 is a leap year

In the above program, the month of February is checked if it contains 29 days.

If a month of February contains 29 days, it will be a leap year.

The new Date(2000, 1, 29) gives the date and time according to the specified arguments.

Tue Feb 29 2000 00:00:00 GMT+0545 (+0545)

The getDate() method returns the day of the month.


Also Read:

Before we wrap up, let’s put your knowledge of JavaScript Program to Check Leap Year to the test! Can you solve the following challenge?

Challenge:

Write a function to check if a year is a leap year.

  • If a year is divisible by 4, it could be a leap year. However, if that year is also divisible by 100, it must also be divisible by 400.
  • For example, 1996 is a leap year because it is divisible by 4 and not divisible by 100.
  • 1900 is not a leap year because, although it is divisible by 4 and 100, it is not divisible by 400.
  • If year is a leap year, return "Leap". Otherwise, return "Noleap".
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges