A leap year is a year that is divisible by 4, but if it is also divisible by 100, it should be divisible by 400 to be a leap year.
In this tutorial, we will write JavaScript programs to check if a given year is a leap year using both simple logic and functions.
1. Basic Leap Year Check Using if…else Statements
Here’s a straightforward JavaScript program to check if a year is a leap year.
// Program to check if a year is a leap year function isLeapYear(year) { if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) { return true; } else { return false; } } // Test the function const year = 2024; if (isLeapYear(year)) { console.log(`${year} is a leap year.`); } else { console.log(`${year} is not a leap year.`); }
Output:
2024 is a leap year
Explanation of the Code
Define the isLeapYear Function:
function isLeapYear(year) { if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) { return true; } else { return false; } }
This function takes a year as an argument.
It uses an if…else statement to check if the year is a leap year using the following conditions:
year % 4 === 0: Checks if the year is divisible by 4.
year % 100 !== 0: Checks if the year is not divisible by 100, which covers the rule that every 100th year is not a leap year unless it is also divisible by 400.
year % 400 === 0: Checks if the year is divisible by 400, making it a leap year.
If either of the conditions is satisfied, the function returns true, indicating it is a leap year.
If none of the conditions are satisfied, it returns false.
Test the Function:
const year = 2024; if (isLeapYear(year)) { console.log(`${year} is a leap year.`); } else { console.log(`${year} is not a leap year.`); }
Calls the isLeapYear function with the year (in this case, 2024).
Uses console.log() to display the result based on the function's return value.
2. Shortened Leap Year Check Using a Ternary Operator
You can also write a more concise version of the leap year check using the ternary operator.
function isLeapYear(year) { return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0); } // Test the function const year = 1900; console.log(`${year} is ${isLeapYear(year) ? 'a leap year' : 'not a leap year'}.`);
Output:
1900 is not a leap year.
Explanation of the Shortened Code
Simplified isLeapYear Function:
This version uses the same condition but directly returns the boolean result of the expression:
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
There’s no need for an if…else statement here, making the code shorter and cleaner.
Using the Ternary Operator:
console.log(`${year} is ${isLeapYear(year) ? 'a leap year' : 'not a leap year'}.`);
Uses the ternary operator (? 🙂 to decide the output based on the return value of isLeapYear(year).
3. Inline Leap Year Check Without a Function
If you don't need to reuse the leap year check and want to keep it inline, here's a quick way to perform the check:
const year = 2000; const isLeap = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0); console.log(`${year} is ${isLeap ? 'a leap year' : 'not a leap year'}.`);
Output:
2000 is a leap year.
Explanation
Directly stores the result of the leap year check in the variable isLeap.
Uses the same condition as before to determine if year is a leap year.
The result is then logged using console.log().
Summary
The logic to check for a leap year in JavaScript follows these steps:
A year is a leap year if it is divisible by 4.
However, if the year is also divisible by 100, it must be divisible by 400 to be a leap year.
This tutorial shows you multiple ways to implement a leap year check in JavaScript:
Using an if…else statement within a function.
Using a concise version with a ternary operator.
Inline check without defining a separate function.
You can use any of these approaches depending on your use case and coding style preferences.