Check if a year is a leap year in JavaScript
Beginner · 4 min read
Every four years, February gets an extra day: 29 February. A year with that extra day is called a leap year. Calendars, booking forms, age calculators and countdown timers all need to know which years are leap years, so this is a small check you will use again and again.
In this tutorial you will write a JavaScript function called isLeapYear. You give it a year, like 2024, and it answers true or false. You only need to know what a function is. Everything else is explained as we go.
The rule, in plain words
Most people know "every four years". That is only part of the rule. Here is the whole thing:
- A year that divides evenly by 4 is a leap year (2024, 2028)...
- ...unless it also divides evenly by 100. Then it is not (1900, 2100)...
- ...unless it also divides evenly by 400. Then it is a leap year after all (2000, 2400).
Why the exceptions? A year on Earth is about 365.2422 days long, a little less than 365 and a quarter. Skipping three leap days every 400 years keeps the calendar in step with the seasons.
Before you start: the % operator
To ask "does this divide evenly?" in JavaScript, you use %, the remainder operator. It gives you what is left over after dividing.
console.log(2024 % 4); // 0: 2024 divides evenly by 4
console.log(2025 % 4); // 1: one left over
console.log(1900 % 100); // 0
A remainder of 0 means "divides evenly". That is the only new idea this exercise needs.
Here is the exercise. If you want to try it on your own first, go ahead: the hints open one at a time. Otherwise, read on and we will build it together.
9 Is it a leap year?
easyDecide whether a given year is a leap year.
Stuck? Three hints, smallest first
- 1. Divisible by 4 is the start, but not the whole rule.
- 2. Century years are not leap years unless they are divisible by 400.
- 3. 1900 was not a leap year; 2000 was. Any answer that gets both wrong is the naive version.
Step 1: check "divisible by 4"
Start with the part of the rule everyone knows.
function isLeapYear(year) {
return year % 4 === 0;
}
console.log(isLeapYear(2024)); // true
console.log(isLeapYear(2025)); // false
year % 4 === 0 is already true or false, so the function can return it directly. You don't need an if.
This looks finished. Now try 1900:
console.log(isLeapYear(1900)); // true, but 1900 was NOT a leap year
That is the bug the exceptions are there to fix.
Step 2: leave out the century years
A year that divides by 100 is a century year. We want "divisible by 4 and not divisible by 100". In JavaScript, "and" is written &&, and "is not equal to" is written !==.
function isLeapYear(year) {
return year % 4 === 0 && year % 100 !== 0;
}
console.log(isLeapYear(1900)); // false: fixed
console.log(isLeapYear(2000)); // false, but 2000 WAS a leap year
1900 is right now. But 2000 has gone wrong, because it is a century year too.
Step 3: let every 400th year back in
The last part of the rule says a year that divides by 400 is always a leap year. "Or" in JavaScript is written ||. So the full rule is: (divisible by 4 and not by 100) or divisible by 400.
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
The brackets group the first two checks, so the line reads the same way the rule does. Now test the years that caught us out:
console.log(isLeapYear(2024)); // true
console.log(isLeapYear(2025)); // false
console.log(isLeapYear(1900)); // false
console.log(isLeapYear(2000)); // true
console.log(isLeapYear(2100)); // false
All five are right. 1900 and 2000 are the two years that trip people up, so always test both.
The full solution
Here is the complete answer. It keeps the naive version from Step 1 so you can see exactly where it goes wrong, and it double-checks the rule against JavaScript's own calendar. Press Run to see the output.
function isLeapYear(year) {
// Divisible by 4, except centuries, except centuries divisible by 400.
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
// The naive version, for comparison.
function naive(year) {
return year % 4 === 0;
}
for (const y of [2024, 2025, 1900, 2000, 2100]) {
console.log(y, isLeapYear(y) ? 'is a leap year' : 'is not');
}
console.log('where the naive version goes wrong:');
console.log(' 1900 correct:', isLeapYear(1900), ' naive:', naive(1900));
console.log(' 2000 correct:', isLeapYear(2000), ' naive:', naive(2000));
// A cross-check against the calendar itself: February has 29 days in a leap year.
function februaryDays(year) {
return new Date(year, 2, 0).getDate(); // day 0 of March is the last of February
}
Output
8 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 8 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('2024 is a leap year', isLeapYear(2024), true);
test('2025 is not', isLeapYear(2025), false);
test('1900 is not, despite dividing by 4', isLeapYear(1900), false);
test('2000 is, because it divides by 400', isLeapYear(2000), true);
test('2100 is not', isLeapYear(2100), false);
test('the naive version gets 1900 wrong', naive(1900), true);
test('the calendar agrees for 2024', februaryDays(2024), 29);
test('and for 1900', februaryDays(1900), 28);
The februaryDays cross-check is worth copying as a technique: rather than trusting the formula, ask the platform. new Date(year, 2, 0) means "day zero of March", which JavaScript resolves to the last day of February. Two independent routes to the same answer is much stronger evidence than one.
Common mistakes
- Stopping at
year % 4 === 0. It gives the right answer for every year from 1901 to 2099, which is why the bug survives so long. It fails for 1900 and 2100. - Writing
=instead of===. A single=stores a value; it doesn't compare.year % 4 = 0is an error. - Leaving out the brackets. JavaScript checks
&&before||, so this line happens to work without them. Keep them anyway: they make the rule easy to read.
Try it yourself
- Write
daysInYear(year)that returns366for a leap year and365for any other year. Use yourisLeapYear. - Use a
forloop to list every leap year from 1890 to 1910. You should get 1892, 1896, 1904 and 1908. Notice that 1900 is missing. - Let JavaScript's calendar decide:
new Date(year, 1, 29).getMonth() === 1istrueonly in a leap year. Work out why. (Hint: what happens to 29 February in a year that doesn't have one?)