Write a JavaScript program to check leap year
Description: Check if a year is a leap year.
Hint:
- Divisible by 4 and not by 100, or divisible by 400.
JavaScript program to check a leap year
function isLeap(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
for (const y of [2024, 1900, 2000, 2023]) {
console.log(y + (isLeap(y) ? " is a leap year" : " is not a leap year"));
}
Output:
Tutorials Class - Output Window
2024 is a leap year
1900 is not a leap year
2000 is a leap year
2023 is not a leap year
How it works
- 1900 is divisible by 100 but not by 400, so it is not a leap year.
- The ternary operator picks the text.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |