0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Check Leap Year with Output

Use the code if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) { /* leap year */ } to check if a year is a leap year in JavaScript.
📋

Examples

Input2000
Output2000 is a leap year
Input1900
Output1900 is not a leap year
Input2024
Output2024 is a leap year
🧠

How to Think About It

To check if a year is leap, first see if it is divisible by 400. If yes, it is a leap year. Otherwise, check if it is divisible by 4 but not by 100. If yes, it is a leap year. Otherwise, it is not a leap year.
📐

Algorithm

1
Get the input year.
2
Check if the year is divisible by 400; if yes, it is a leap year.
3
Else, check if the year is divisible by 4 but not by 100; if yes, it is a leap year.
4
Otherwise, it is not a leap year.
5
Return the result.
💻

Code

javascript
function isLeapYear(year) {
  if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
    return `${year} is a leap year`;
  } else {
    return `${year} is not a leap year`;
  }
}

console.log(isLeapYear(2000));
console.log(isLeapYear(1900));
console.log(isLeapYear(2024));
Output
2000 is a leap year 1900 is not a leap year 2024 is a leap year
🔍

Dry Run

Let's trace the year 1900 through the code

1

Check divisibility by 400

1900 % 400 === 300 (false)

2

Check divisibility by 4 and not by 100

1900 % 4 === 0 (true), 1900 % 100 === 0 (true), so condition false

3

Result

1900 is not a leap year

StepConditionResult
11900 % 400 === 0false
21900 % 4 === 0 && 1900 % 100 !== 0false
3Return not leap year1900 is not a leap year
💡

Why This Works

Step 1: Divisible by 400 means leap year

If a year divides evenly by 400, it is always a leap year, so we check this first.

Step 2: Divisible by 4 but not 100 means leap year

If a year divides by 4 but not by 100, it is a leap year because century years are special.

Step 3: Otherwise not a leap year

If neither condition is true, the year is not a leap year.

🔄

Alternative Approaches

Using Date object
javascript
function isLeapYear(year) {
  return new Date(year, 1, 29).getDate() === 29 ? `${year} is a leap year` : `${year} is not a leap year`;
}

console.log(isLeapYear(2000));
console.log(isLeapYear(1900));
console.log(isLeapYear(2024));
This method uses JavaScript's Date object to check if February 29 exists in the year, which is simple but less explicit.
Using nested if statements
javascript
function isLeapYear(year) {
  if (year % 400 === 0) {
    return `${year} is a leap year`;
  } else if (year % 100 === 0) {
    return `${year} is not a leap year`;
  } else if (year % 4 === 0) {
    return `${year} is a leap year`;
  } else {
    return `${year} is not a leap year`;
  }
}

console.log(isLeapYear(2000));
console.log(isLeapYear(1900));
console.log(isLeapYear(2024));
This approach uses clear nested conditions, which some find easier to read but is longer.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic operations and comparisons, so it runs in constant time.

Space Complexity

The program uses a fixed amount of memory for variables and returns a string, so space is constant.

Which Approach is Fastest?

All approaches run in constant time, but using arithmetic checks is more direct and clear than using Date objects.

ApproachTimeSpaceBest For
Arithmetic checksO(1)O(1)Clarity and performance
Date object checkO(1)O(1)Simplicity but less explicit
Nested if statementsO(1)O(1)Readability for beginners
💡
Always check divisibility by 400 before 100 to correctly identify leap years.
⚠️
Forgetting that years divisible by 100 are not leap years unless divisible by 400.