JavaScript Program to Check Leap Year

Updated on November 8, 2024
Check leap year header image

Introduction

A Leap Year in the Gregorian calendar occurs approximately every four years and is crucial for keeping the calendar year synchronized with the astronomical year. Determining whether a year is a leap year is essential in various computing tasks, especially those related to date and time calculations.

In this article, you will learn how to determine if a given year is a leap year using JavaScript. Explore different methods to implement this check, including straightforward conditional checks and more streamlined approaches using logical operators.

Basics of Leap Year Calculation

A year is considered a leap year if it meets certain conditions:

  • The year is evenly divisible by 4;
  • If the year can be evenly divided by 100, it is NOT a leap year, unless;
  • The year is also evenly divisible by 400. Then it is a leap year.

Example: Simple Conditional Check

Implement a function to check if a year is a leap year by applying the conditions mentioned above.

  1. Define a function named isLeapYear.

  2. Use if-else statements to check if the year meets the leap year conditions.

    javascript
    function isLeapYear(year) {
        if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
            return true;
        } else {
            return false;
        }
    }
    
    console.log(isLeapYear(2024)); // Output: true
    console.log(isLeapYear(1900)); // Output: false
    

    This function checks if the year is divisible by 4 but not by 100 unless it's also divisible by 400. If these conditions are satisfied, the function returns true indicating a leap year, otherwise false.

Condensed Ternary Operator Method

Utilize a ternary operator for a more condensed version of the leap year check.

  1. Use the ternary operator to shorten the conditional logic into a single line.

  2. Implement the same logic as before but in a more concise format.

    javascript
    const isLeapYearTernary = year => (year % 400 === 0) ? true : (year % 100 === 0) ? false : (year % 4 === 0);
    
    console.log(isLeapYearTernary(2024)); // Output: true
    console.log(isLeapYearTernary(1900)); // Output: false
    

    This concise function uses a chained ternary operator to perform the leap year calculation in a more readable and compact form, especially useful for embedding directly into other code where brevity is advantageous.

Conclusion

Determining whether a given year is a leap year is a common task that can be efficiently executed using simple JavaScript functions. With the methods discussed, you can easily integrate leap year checks into any date handling operations in your JavaScript applications. Whether you use the detailed conditional approach or the streamlined ternary operation, both methods provide clear, effective results for leap year evaluation. Equip your projects with accurate date calculations by integrating these leap year functions as needed.