
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Given Year is a Leap Year in JavaScript
To check if a given year is leap year, We have used two approaches in this article. A leap year has 366 days. In every 4th year, an additional day is added to the month of February to synchronize it with astronomical year.
In this article, we are given with two years to check. Our task is to write a JavaScript program to check if a given year is leap year.
Approaches to Check Leap Year
Here is a list of approaches to check if a given year is leap year in Javascript which we will be discussing in this article with stepwise explanation and complete example codes.
Using if-else Statement
In the if-else conditional statement, the if block is executed when the specified condition is TRUE, otherwise, the optional else block is executed.
- We have specifies two years which we are going to check.
- Then we have used if/else statement to check if the specified years follow the condition to be a leap year.
- The conditions are: the given year must be divisible by 4 and not divisible 100 or must be divisible by 400.
Example
Here is a complete example code implementing above mentioned steps to check if a given year is leap year in Javascript using if/else statement.
let year1 = 2021; let year2 = 2024; function isLeapYear(year) { if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)){ console.log(`${year} is a leap year.`); } else { console.log(`${year} is not a leap year.`); } } isLeapYear(year1); isLeapYear(year2);
Using Date Object
In this approach to check if a given year is leap year we have used Date object.
- We have declared two variables for checking if the years are leap year or not. We have also created a function isLeapYear() which accepts year as parameter.
- Then we have used new Date(year, 1, 29) which checks if there is 29 day in the month of February of the given year.
- We have used getDate() method to get check whether February 29 exist in the specified year.
- Then the result is displayed in web console using console.log() method depending on the value of getDate() method.
Example
Here is a complete example code implementing above mentioned steps to check if a given year is leap year in Javascript using Date object.
let year1 = 2021; let year2 = 2024; function isLeapYear(year) { const date = new Date(year, 1, 29); if (date.getDate() === 29) { console.log(`${year} is a leap year.`); } else { console.log(`${year} is not a leap year.`); } } isLeapYear(year1); isLeapYear(year2);
Conclusion
In this article to write a JavaScript program to check if a given year is leap year, we have used two different approaches. These approaches are: by using if/else statement and Date object.
Practice and learn from a wide range of JavaScript examples, including event handling, form validation, and advanced techniques. Interactive code snippets for hands-on learning.