Traditional Leap Year Logic
Our approach will be to verify the leap-year conditions mentioned above.
- First, Check the given input year is a multiple of 400. If true give output “Input year is a Leap year”.
- If false, Check for the secondary conditions that the input year must be a multiple of 4 but not 100.
- Then display the output accordingly.
Example 1: In this example, we will check the conditions of Leap year for previous years
function isLeapYear(year) {
if (
year % 100 === 0 ? year % 400 === 0 : year % 4 === 0
)
console.log(" Input year:", year, "is a Leap Year");
else
console.log(
" Input year:",
year,
"is NOT a Leap Year"
);
}
let inputYear = 2020;
isLeapYear(inputYear);
inputYear = 2023;
isLeapYear(inputYear);
Output
Input year: 2020 is a Leap Year Input year: 2023 is NOT a Leap Year
Example 2: In this example, we will check the conditions of Leap year for next/ upcoming years
function isLeapYear(year) {
if (
year % 100 === 0 ? year % 400 === 0 : year % 4 === 0
)
console.log(" Input year:", year, "is a Leap Year");
else
console.log(
" Input year:",
year,
"is NOT a Leap Year"
);
}
let inputYear = 2354;
isLeapYear(inputYear);
inputYear = 2640;
isLeapYear(inputYear);
Output
Input year: 2354 is NOT a Leap Year Input year: 2640 is a Leap Year
JavaScript Program to Check if a Given Year is Leap Year
We will explore how to determine whether a given year is a leap year or not. Leap years, which occur approximately every four years, have special conditions that must be met for validation.
To identify a leap year, two conditions must be satisfied:
- The year must be a multiple of 400.
- Alternatively, if the year is a multiple of 4 and not a multiple of 100, it’s also considered a leap year.
By understanding and implementing these conditions in our JavaScript program, we’ll be able to accurately determine whether any given year qualifies as a leap year.
Below are the approaches:
Table of Content
- Traditional Leap Year Logic
- Using newDate()
Contact Us