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:
// ONLINE-RUNNER:browser;
const isLeap = (year) => {
return year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0);
}
console.log(isLeap(2000)); // true
console.log(isLeap(2001)); // false
Practical example
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:
// ONLINE-RUNNER:browser;
function isLeap(year) {
if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
return true;
}
return false;
}
console.log(isLeap(2000)); // true
console.log(isLeap(2001)); // false
console.log(isLeap(2002)); // false
console.log(isLeap(2003)); // false
console.log(isLeap(2004)); // true
Output:
true
false
false
false
true