Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Leap Year Check in JavaScript

Check for a leap year in JavaScript with the 4, 100 and 400 rules. Three methods, test years 2000, 1900, 2023 and 2024, plus a clear explanation and output.

BeginnerJavaScript Conditional ProgramsExample 4 of 5
leap-year-check.js
1// Leap year rules:
2// 1. Divisible by 4 → Leap year
3// 2. BUT if divisible by 100 → NOT leap year
4// 3. UNLESS divisible by 400 → Leap year
5
6function isLeapYear(year) {
7 if (year % 400 === 0) {
8 return true;
9 } else if (year % 100 === 0) {
10 return false;
11 } else if (year % 4 === 0) {
12 return true;
13 } else {
14 return false;
15 }
16}
17
18// Test cases
19console.log("2000:", isLeapYear(2000) ? "Leap year" : "Not leap year");
20console.log("1900:", isLeapYear(1900) ? "Leap year" : "Not leap year");
21console.log("2024:", isLeapYear(2024) ? "Leap year" : "Not leap year");
22console.log("2023:", isLeapYear(2023) ? "Leap year" : "Not leap year");
23
24// Method 2: Single expression
25function isLeapYear2(year) {
26 return (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0);
27}
28
29console.log("\nUsing single expression:");
30console.log("2000:", isLeapYear2(2000));
31console.log("1900:", isLeapYear2(1900));
32console.log("2024:", isLeapYear2(2024));
33
34// Method 3: Using ternary
35function isLeapYear3(year) {
36 return year % 400 === 0 ? true :
37 year % 100 === 0 ? false :
38 year % 4 === 0 ? true : false;
39}
40
41console.log("\nUsing ternary:");
42console.log("2000:", isLeapYear3(2000));
43console.log("2023:", isLeapYear3(2023));

Sample Output

2000: Leap year
1900: Not leap year
2024: Leap year
2023: Not leap year

Using single expression:
2000: true
1900: false
2024: true

Using ternary:
2000: true
2023: false

In this one

  • The full leap year rule
  • Why the order of checks matters
  • How && and || combine conditions
  • How to pick test values that cover every branch

How it works

  1. 1

    2000 % 400 is 0, so true.

  2. 2

    1900 is not divisible by 400 but is by 100, so false.

  3. 3

    2024 passes the % 4 check, so true.

  4. 4

    2023 fails every check, so false.

  5. 5

    The one-line and ternary versions repeat these tests.

Checking 1900

Follow isLeapYear(1900) through each branch.

  1. 1.1900 % 400 = 300, not 0, so skip.
  2. 2.1900 % 100 = 0, so return false.
  3. 3.The % 4 check is never reached.

Edge cases

Only checking % 4

1900 and 2100 would be called leap years.

Century years need the extra 100 and 400 rules.

Year as a string

isLeapYear("2024") still works because % converts strings.

Convert input with Number() anyway so other checks are reliable.

What's going on

A leap year has 366 days. February gets a 29th day. This program decides if a given year is a leap year. Calendar apps, date pickers and exam-date checkers all need this.

The rule has three parts. A year divisible by 4 is a leap year. But a year divisible by 100 is not. Unless it is also divisible by 400, then it is a leap year again. So 2024 is a leap year, 1900 is not, and 2000 is.

Order of checks matters. Method 1 checks % 400 first, then % 100, then % 4. If you only test % 4, then 1900 wrongly becomes a leap year. If 2023 shows as a leap year, your code is not even testing % 4 correctly.

Method 2 writes the whole rule in one line: (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0). Read it as "divisible by 400, or divisible by 4 but not by 100". && means "and". || means "or".

Method 3 chains ternary operators. Each ? asks a question and each : gives the next step. It works, but nested ternaries get hard to read fast. Most people prefer Method 2.

The test years 2000, 1900, 2024 and 2023 cover every branch of the rule.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Is 1900 a leap year?

Pick an answer to check it.

Question 2

Is 2000 a leap year?

Pick an answer to check it.

Questions people ask

Why do we need leap years?

A year is about 365.2422 days. The extra day keeps the calendar in line with the seasons.

Will 2100 be a leap year?

No. It is divisible by 100 but not by 400.

Can the Date object check this?

Yes. new Date(year, 1, 29).getDate() === 29 is true only in leap years.