javascript

checkLeapYear()

Parameters: year (integer number)

The year to check if it is a leap year

Returns: Boolean value to indicate if the input year is a leap year

This JavaScript function takes a year as input and determines if it's a leap year or not. A year is considered as a leap year if it is divisible by 4 except for end of century years which have to be divisible by 400. This means that the year 2000 was considered a leap year while 1900 was not.

Variables
Conditionals
Modulus Operator
Boolean
Medium dificulty

Writing a JavaScript function to check for Leap Year

Hello programmer, welcome to this blog post. Today, we will be exploring how to create a function in JavaScript to determine if a year is a leap year or not. This function is commonly called checkLeapYear. We'll guide you through each step in the programming process, making sure it's clear and easy to follow. We promise, no techno-babble or insider jargon, just plain and simple English. Let's get started!

Step 1: Define the Function Structure

The first step in generating this function is to define your function structure. This means naming your function and determining what parameters it will accept. Our function will be named checkLeapYear and it will receive a single parameter year which is the year that we want to check whether it is a leap year or not.

function checkLeapYear(year) {
  
}

Step 2: Divisible by 4

A year is a leap year if it is perfectly divisible by 4. So we need to check if the year provided to the function is divisible by 4. If it is, that's the first condition satisfying the leap year rule. For this, we will use the modulo operator %.

function checkLeapYear(year) {
  if(year % 4 === 0) {
    
  }
}

Step 3: Divisible by 100 but not by 400

The superstition to the rule comes in here: if the year is also divisible by 100, it is not a leap year, unless it is also divisible by 400. This means that years 1700, 1800, and 1900 were not leap years, but 1600 and 2000 were because they are divisible by 400. Hence, this gives us the two conditions to check inside our first if statement.

function checkLeapYear(year) {
  if(year % 4 === 0) {
    if(year % 100 === 0) {
      if(year % 400 === 0) {
        return true;  // is a leap year
      }
      return false;
    }
    return true; // is a leap year
  }
  return false;  
}

Step 4: Return Statements

If we got to the last statement without returning anything yet, it's clear that this year is not divisible by 4, hence it is not a leap year, and we should return false.

function checkLeapYear(year) {
  if(year % 4 === 0) {
    if(year % 100 === 0) {
      if(year % 400 === 0) {
        return true;  // is a leap year
      }
      return false;
    }
    return true; // is a leap year
  }
  return false;  // not a leap year 
}

Step 5: Function Call

Voila, our function is ready. Let's test this function by calling it and passing in different years to check whether they are leap years or not.

console.log(checkLeapYear(2020)); // 2020 is a leap year, so it should return true
console.log(checkLeapYear(2021)); // 2021 is not a leap year, so it should return false
console.log(checkLeapYear(1700)); // 1700 is not a leap year, so it should return false
console.log(checkLeapYear(2000)); // 2000 is a leap year, so it should return true

In conclusion, the checkLeapYear function uses conditional statements to determine whether a given year is a leap year or not according to the Gregorian calendar rules. This is a simple yet efficient way to carry out this task.

Learn function in:

Leap Year Checking

Determines if a year is a leap year or not

Learn more

Mathematical principle

The detection of a leap year is based on the Gregorian calendar system. According to this system, a year is considered a leap year if it is perfectly divisible by 4. However, centuries (years ending with 00) are an exception where they are considered leap years if and only if they are divisible by 400. This is implemented by using modulus operation (`%`) which gives the remainder of division of two numbers.

Learn more