JS Basic
JS Interview Programs
- JS Interview Programs
- JS Abundant Number
- JS Amicable Number
- JS Armstrong Number
- JS Average of N Numbers
- JS Automorphic Number
- JS Biggest of three numbers
- JS Binary to Decimal
- JS Common Divisors
- JS Composite Number
- JS Condense a Number
- JS Cube Number
- JS Decimal to Binary
- JS Decimal to Octal
- JS Disarium Number
- JS Even Number
- JS Evil Number
- JS Factorial of a Number
- JS Fibonacci Series
- JS GCD
- JS Happy Number
- JS Harshad Number
- JS LCM
- JS Leap Year
- JS Magic Number
- JS Matrix Addition
- JS Matrix Division
- JS Matrix Multiplication
- JS Matrix Subtraction
- JS Matrix Transpose
- JS Maximum Value of an Array
- JS Minimum Value of an Array
- JS Multiplication Table
- JS Natural Number
- JS Number Combination
- JS Odd Number
- JS Palindrome Number
- JS Pascalβs Triangle
- JS Perfect Number
- JS Perfect Square
- JS Power of 2
- JS Power of 3
- JS Pronic Number
- JS Prime Factor
- JS Prime Number
- JS Smith Number
- JS Strong Number
- JS Sum of Array
- JS Sum of Digits
- JS Swap Two Numbers
- JS Triangular Number
JavaScript Program to Check Leap Year
Photo Credit to CodeToFun
π Introduction
In the realm of programming, determining whether a given year is a leap year is a common requirement. A leap year is a year that is evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year. Checking for leap years involves implementing specific rules to validate the year.
In this tutorial, we will explore a JavaScript program designed to check whether a given year is a leap year.
π Example
Let's delve into the JavaScript code that accomplishes this task.
// Function to check if a year is a leap year
function isLeapYear(year) {
// Leap year conditions
if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0))
return true; // True, it's a leap year
else
return false; // False, it's not a leap year
}
// Driver program
// Replace this value with the year you want to check
const year = 2024;
// Call the function to check if the year is a leap year
if (isLeapYear(year))
console.log(`${year} is a leap year.`);
else
console.log(`${year} is not a leap year.`);
π» Testing the Program
To test the program with different years, modify the value of year in the code.
2024 is a leap year.
π§ How the Program Works
- The program defines a function isLeapYear that takes an integer year as input and returns true if the year is a leap year, and false otherwise.
- The function checks leap year conditions: the year must be divisible by 4 and not divisible by 100, or it must be divisible by 400.
- Replace the value of year in the main program with the desired year you want to check.
- The program calls the isLeapYear function and prints the result using console.log.
π Between the Given Range
Let's dive into the javascript code that checks for leap years in the specified range.
// Function to check if a year is a leap year
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}
// Function to find leap years in the range
function findLeapYearsInRange(startYear, endYear) {
let leapYears = [];
for (let year = startYear; year <= endYear; year++) {
if (isLeapYear(year)) {
leapYears.push(year);
}
}
return leapYears;
}
// Driver program
const startYear = 2024;
const endYear = 2050;
// Call the function to find leap years in the range
const leapYearsInRange = findLeapYearsInRange(startYear, endYear);
// Output the result
console.log(`Leap Years in the range ${startYear} to ${endYear}:`);
console.log(leapYearsInRange.join(" "));
π» Testing the Program
The provided code is already set to check for leap years in the range from 2024 to 2050.
Leap years in the range 2024 to 2050: 2024 2028 2032 2036 2040 2044 2048
Run the script to see the leap years in the specified format.
π§ How the Program Works
- The program defines a function isLeapYear that checks if a given year is a leap year.
- It then defines a function findLeapYearsInRange that iterates through the specified range and collects leap years.
- The main section tests the program by specifying the range from 2024 to 2050 and outputs the result in the specified format.
π§ Understanding the Concept of Leap Year
Before delving into the code, let's understand the concept of leap years. Leap years are years that are evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing alternative approaches or optimizations for checking leap years.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (JavaScript Program to Check Leap Year), please comment here. I will help you immediately.