Leap Year Check

Program to check if a year is a leap year

BeginnerTopic: Conditional Programs
Back

JavaScript Leap Year Check Program

This program helps you to learn the fundamental structure and syntax of JavaScript programming.

Try This Code
// Leap year rules:
// 1. Divisible by 4 → Leap year
// 2. BUT if divisible by 100 → NOT leap year
// 3. UNLESS divisible by 400 → Leap year

function isLeapYear(year) {
    if (year % 400 === 0) {
        return true;
    } else if (year % 100 === 0) {
        return false;
    } else if (year % 4 === 0) {
        return true;
    } else {
        return false;
    }
}

// Test cases
console.log("2000:", isLeapYear(2000) ? "Leap year" : "Not leap year");
console.log("1900:", isLeapYear(1900) ? "Leap year" : "Not leap year");
console.log("2024:", isLeapYear(2024) ? "Leap year" : "Not leap year");
console.log("2023:", isLeapYear(2023) ? "Leap year" : "Not leap year");

// Method 2: Single expression
function isLeapYear2(year) {
    return (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0);
}

console.log("\nUsing single expression:");
console.log("2000:", isLeapYear2(2000));
console.log("1900:", isLeapYear2(1900));
console.log("2024:", isLeapYear2(2024));

// Method 3: Using ternary
function isLeapYear3(year) {
    return year % 400 === 0 ? true : 
           year % 100 === 0 ? false : 
           year % 4 === 0 ? true : false;
}

console.log("\nUsing ternary:");
console.log("2000:", isLeapYear3(2000));
console.log("2023:", isLeapYear3(2023));
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

Understanding Leap Year Check

This program demonstrates leap year logic with multiple conditions.

Leap Year Rules

A year is a leap year if:

1.It's divisible by 400 →

Leap year

2.OR it's divisible by 4 BUT NOT by 100 →

Leap year

3.Otherwise →

Not a leap year

Examples:

2000: Divisible by 400 → ✅ Leap year
1900: Divisible by 100 but not 400 → ❌ Not leap year
2024: Divisible by 4, not by 100 → ✅ Leap year
2023: Not divisible by 4 → ❌ Not leap year

Method 1: If-Else Chain

Clear, step-by-step logic:

if (year % 400 === 0) return true;
else if (year % 100 === 0) return false;
else if (year % 4 === 0) return true;
else return false;

Method 2: Single Expression

Combining conditions with logical operators:

return (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0);

Logical Operators:

||: OR - at least one true
&&: AND - both must be true
!: NOT - reverses boolean

Method 3: Nested Ternary

Compact but less readable:

       year % 100 === 0 ? false : 
       year % 4 === 0 ? true : false;
return year % 400 === 0 ? true : 

Why Leap Years?

Earth's orbit: ~365.25 days
Extra 0.25 days accumulate
Every 4 years: +1 day (February 29)
Exception: Century years need 400 rule

When to Use:

-

If-else

: Most readable, learning

-

Single expression

: Concise, efficient

-

Ternary

: Compact, but harder to read

Let us now understand every line and the components of the above program.

Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.

Practical Learning Notes for Leap Year Check

This JavaScript program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents