Master Leap Years in JavaScript: A Complete Guide

Ever pondered why every four years, we enjoy an extra day? That's the magic of leap years, astronomically aligned to synchronize our calendar with Earth's rotation around the Sun. But how can we implement this in JavaScript? Let's dive in!

Leap Year Program in Python 📅 | Beginner Logic Explained
Leap Year Program in Python 📅 | Beginner Logic Explained

The first step is understanding leap years. They are the years that are divisible by 4, but not all years that meet this rule qualify. To determine a leap year in JavaScript, a more specific rule comes into play: a year is a leap year if it is divisible by 4, but not divisible by 100, unless it's also divisible by 400.

Coding class
Coding class

Defining Leap Years

Let's write a JavaScript function to determine if a given year is a leap year:

a screen shot of a computer program with the text python program to check whether given year is a leap year
a screen shot of a computer program with the text python program to check whether given year is a leap year

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

Using the Function

an image of a programming program with the text c + + program to check leap year
an image of a programming program with the text c + + program to check leap year

This function takes a year as an argument and returns `true` if the year is a leap year, and `false` otherwise.

Let's check if an input year is a leap year in an interactive way:

const year = prompt("Enter a year:");
alert(`The year ${year} is ${isLeapYear(year) ? '' : 'not '}a leap year.`);

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had

Handle Inputs

When prompting for user input, it's essential to handle invalid inputs, such as non-numeric values or years out of the valid range (1 - 9999).

Here's an-enhanced version of the above script to handle these edge cases:

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

const year = prompt("Enter a year:");
if (!isNaN(year) && year >= 1 && year <= 9999) {
  alert(`The year ${year} is ${isLeapYear(year) ? '' : 'not '}a leap year.`);
} else {
  alert("Invalid input! Please enter a year between 1 and 9999.");
}

Next Leap Year

Learn JavaScript in 30 Days
Learn JavaScript in 30 Days
JavaScript Full Notes Every Beginner Must Save 💡
JavaScript Full Notes Every Beginner Must Save 💡
Javascript Roadmap
Javascript Roadmap
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript Cheat Sheet 🚀 | Complete JS Reference for Beginners & Developers
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
leap year program in java language for btech students in a easy approach.
leap year program in java language for btech students in a easy approach.
🚀 Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! 💻✨
🚀 Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! 💻✨
JavaScript Loops Cheat Sheet for Beginners
JavaScript Loops Cheat Sheet for Beginners
Simple JavaScript Countdown Part 2
Simple JavaScript Countdown Part 2
JavaScript loops by Duomly
JavaScript loops by Duomly
Javascript Fundamental
Javascript Fundamental
Javascript basic events
Javascript basic events
an image of a computer screen showing the time and date for different things on it
an image of a computer screen showing the time and date for different things on it
JavaScript Roadmap for Beginners 2026
JavaScript Roadmap for Beginners 2026
Top JavaScript Tricks Every Developer Should Know
Top JavaScript Tricks Every Developer Should Know
a flyer for an upcoming event with information about the date and time in each section
a flyer for an upcoming event with information about the date and time in each section
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳
Cheat Cheat on JavaScript Loops
Cheat Cheat on JavaScript Loops
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
JavaScript Web Development Cheat Sheet: Your Essential Guide - Connect 4 Programming
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
🚀 JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide

Another useful function could be finding the next leap year after an input year:

function nextLeapYear(year) {
  year++;
  while (year <= 9999 && !isLeapYear(year)) {
    year++;
  }
  return year;
}

Predicting the Future

Let's use this function to find the next leap year, assuming we've input '2025' as the current year:

const currentYear = 2025;
console.log(`The next leap year after ${currentYear} is ${nextLeapYear(currentYear)}.`);

And there you have it! We've created simple yet effective functions to handle leap years in JavaScript. Whether you're just curious about when the next one is or need it for a project, these functions should serve you well.

Related Articles

W3schools Js Sandbox What Does Yield Do In Javascript Leap Year Js Every W3schools Javascript Input Validation Html Css Javascript Xml W3schools Javascript Window Location Href W3schools Test Regex Window Close Javascript W3schools Javascript Regex Validate Email Address Window Event In Javascript W3schools