Ad space (banner)
🟨JavaScript Lessons
Lesson 66 / 69

Checking Whether a Year Is a Leap Year

In this lesson you'll learn the logic for determining whether a year is a leap year, so you can understand how to correctly translate a rule that seems simple at first glance, but has exceptions, into code. This is for people searching "JavaScript leap year check" who landed here.

A year is a leap year if it's divisible by 4 but not divisible by 100, or if it's divisible by 400. This is a basic piece of logic used often in calendar and date-calculation programs. What's distinctive is that judging based only on "a multiple of 4" leads to a mistake — a classic subject for practicing how to correctly assemble a conditional.

The sample code determines whether a year is a leap year using a single condition, (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0. Confirm the result for a few examples: 2024 is a leap year because it's a multiple of 4 but not a multiple of 100; 1900 is not a leap year because, even though it's a multiple of 4, it's also a multiple of 100 (and not a multiple of 400).

A common beginner stumbling block is overlooking the exception for years divisible by 100. Implementing this with only the simple rule "a multiple of 4 is a leap year" incorrectly judges years like 1900 as leap years. This is good practice for correctly translating a rule that "looks simple at first glance, but has exceptions" into code.

This is foundational knowledge for programs that handle dates, like a calendar app or age calculation. Accurate date logic is essential foundational knowledge required even in actual business systems, like a reservation system or calculating an invoice's closing date.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)