Leap year rule (divisible by 4, 100, 400) in JavaScript
Beginner · 3 min read
A leap year has 366 days instead of 365, because February gets a 29th day. Any code that counts days, plans events or works out ages has to know which years those are. In this tutorial you will write the rule as a JavaScript function, isLeapYear, and then do something many tutorials skip: check your rule against JavaScript's own calendar, for 901 years in a row.
You need to know what a function is and how && (and), || (or) and === work. Everything else is explained as we go.
The full rule
"Every four years" is only part of it. The full rule has three parts:
- A year that divides evenly by 4 is a leap year (2024, 2028).
- Except a year that also divides by 100. That one is not (1900, 2100).
- Except a year that also divides by 400. That one is a leap year after all (2000, 2400).
The exceptions exist because a year is a little shorter than 365 and a quarter days. Dropping three leap days every 400 years keeps the calendar in line with the seasons.
Checking "divides evenly"
The remainder operator % gives what is left after dividing. A remainder of 0 means the number divides evenly:
console.log(2024 % 4, 2023 % 4, 1900 % 100); // 0 3 0
Here is the exercise. Have a go on your own first if you like, then read on and we will build it together.
9 Is this a leap year?
easyImplement the leap-year rule: every 4 years, except every 100, except every 400. Check it against 1900, 2000 and 2100.
Stuck? Three hints, smallest first
- 1. Three conditions, not one. Divisible by 4 alone is not enough.
- 2. Divisible by 100 cancels the rule; divisible by 400 brings it back.
- 3. Cross-check your answer against what the
Dateconstructor thinks 29 February is.
Step 1: the first part, and why it is not enough
const fourOnly = (year) => year % 4 === 0;
console.log(fourOnly(2024), fourOnly(2023)); // true false
console.log(fourOnly(1900)); // true, but 1900 was not a leap year
This looks finished, and it is right for every year from 1901 to 2099. That long run of correct answers is why so much old software shipped with only this check. 1900 shows the bug, and 2100 will show it again.
Step 2: the whole rule in one line
Divisible by 4 and not by 100, or divisible by 400:
const isLeapYear = (year) => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
for (const y of [1900, 2000, 2023, 2024, 2100, 2400]) {
console.log(y, isLeapYear(y));
}
// 1900 false
// 2000 true
// 2023 false
// 2024 true
// 2100 false
// 2400 true
The brackets group the first two checks so the line reads in the same order as the rule. 1900 and 2100 are out; 2000 and 2400 are back in.
Step 3: ask the calendar instead
Spot checks are good, but six years is a small sample. Is there a second, independent way to find leap years? There is, and it uses a habit of new Date: an impossible day doesn't fail, it rolls into the next month. So try to build 29 February (month 1, day 29). If the year has no 29 February, you get 1 March instead:
const hasFeb29 = (year) => new Date(year, 1, 29).getDate() === 29;
console.log(hasFeb29(2024)); // true
console.log(hasFeb29(1900)); // false
console.log(new Date(1900, 1, 29).toDateString()); // 'Thu Mar 01 1900'
getDate() reads the day of the month. If it is still 29, the day existed.
Step 4: compare the two for 901 years
Now let the computer check every year from 1600 to 2500:
const years = Array.from({ length: 901 }, (unused, i) => 1600 + i);
console.log(years[0], years[900]); // 1600 2500
console.log(years.every((y) => isLeapYear(y) === hasFeb29(y))); // true
Array.from({ length: 901 }, fn) builds a list of 901 items, and fn receives each position i, so 1600 + i runs through the years. every returns true only if the test passes for every single item.
Two methods written in completely different ways agree across nine centuries. That is far stronger evidence than a handful of examples, and it is a habit worth copying: when you can check your code against a second, independent method, do it.
The full solution
The panel has isLeapYear, hasFeb29, a table of six years and the 901-year comparison as its last check. Press Run to see it.
const isLeapYear = (year) => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
// An independent check: if 29 February exists, the date does not roll into March.
const hasFeb29 = (year) => new Date(year, 1, 29).getDate() === 29;
for (const y of [1900, 2000, 2023, 2024, 2100, 2400]) {
console.log(y, isLeapYear(y) ? 'leap' : 'common', '| 29 Feb exists:', hasFeb29(y));
}
Output
9 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 9 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('2023 is not', isLeapYear(2023), false);
test('1900 is not, because it is divisible by 100', isLeapYear(1900), false);
test('2000 is, because it is divisible by 400', isLeapYear(2000), true);
test('2100 will not be', isLeapYear(2100), false);
test('2400 will be', isLeapYear(2400), true);
test('the constructor agrees about 2024', hasFeb29(2024), true);
test('and about 1900', hasFeb29(1900), false);
test('the two rules never disagree, 1600 to 2500', Array.from({ length: 901 }, (unused, i) => 1600 + i).every((y) => isLeapYear(y) === hasFeb29(y)), true);
The 100 and 400 clauses are not trivia. A calendar that only checks year % 4 === 0 was correct for every year between 1901 and 2099, which is why so much software shipped with the bug and why 2100 will be interesting.
The last assertion is worth the loop: it checks the hand-written rule against the engine's own calendar for 901 consecutive years. Two independent implementations agreeing across nine centuries is a much stronger claim than four spot checks.
Common mistakes
- Stopping at
year % 4 === 0. It is right for 1901 to 2099 and wrong for 1900 and 2100. - Writing
year % 100 === 0in the first part. The century test must say "not divisible", so it is!== 0. - Checking
hasFeb29with month 2. Months count from 0, so month 2 is March, and every year has a 29 March.
Try it yourself
- List the leap years between 2090 and 2110. 2100 should not be there.
- Count how many leap years there are from 2001 to 2400. (Hint:
filterandlength.) - Write
nextLeapYear(year)that returns the first leap year afteryear. What does it give for 2096?