EN
JavaScript - check if specific year is leap
0 points
In this article, we would like to show you how to check if the specific year is leap in JavaScript.
Quick solution:
xxxxxxxxxx
1
const isLeap = (year) => {
2
return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0);
3
}
4
5
console.log(isLeap(2000)); // true
6
console.log(isLeap(2001)); // false
In this example, we check if the specific year is leap with the following conditions:
- check if the year is a multiple of
400
, - or if it's a multiple of
4
and not multiple of100
.
Then we return the appropriate value true or false depending on the result.
Runnable example:
xxxxxxxxxx
1
function isLeap(year) {
2
if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
3
return true;
4
}
5
return false;
6
}
7
8
console.log(isLeap(2000)); // true
9
console.log(isLeap(2001)); // false
10
console.log(isLeap(2002)); // false
11
console.log(isLeap(2003)); // false
12
console.log(isLeap(2004)); // true
Output:
xxxxxxxxxx
1
true
2
false
3
false
4
false
5
true