Check Whether or Not the Year is a Leap Year in Javascript?

Check Whether or Not the Year is a Leap Year in Javascript?

We will discuss four methods to solve this problem using Javascript

Introduction

Hi everyone today we find Check Whether or Not the Year is a Leap Year in Javascript. let's discuss how to solve this problem statement. if you want to check out yesterday's problem click here.

The goal is to develop a Javascript code to get the largest of the three numbers given three integer inputs, num1, num2, and num3. We'll explore a Javascript program to find the greatest of three integers in this post. To discover the same, we will also utilize the ternary operator and if else conditions. The following are various solutions to the difficulty indicated above:

  • Method 1: Using if-else Statements 1

  • Method 2: Using if-else Statements 2

  • Method 3: Using Ternary Operator

  • Method 4: Boolean method

We’ll discuss the above-mentioned methods in detail in the upcoming sections. Before we get to coding check the blue box below for a better understanding.

Conditions for Leap Year

For any year to be a leap year the following two conditions must be checked. If the year satisfies either of the conditions, it's considered a leap year. Following are the conditions
1. The year must be divisible by 400.
2. The year must be divisible by 4 but not 100.

Method 1: Using if-else Statements 1

Algorithm

  • step 1: Take the user input year and declare const.

  • step 2: Check if the year variable is divisible by 400 or not if yes year is a leap year

  • step 3: check if the year is divisible by 4 and the year is not divisible by 100 then the print year is a leap year if not year is not a leap year

Javascript Code

const year = 2020;

if (year % 400 === 0) {
  console.log(year + " is a Leap Year");
} else if (year % 4 === 0 && year % 100 !== 0) {
  console.log(year + " is a Leap Year");
} else {
  console.log(year + " is not a Leap Year");
}

Output

2020 is a Leap Year

Method 2: Using if-else Statements 2

Algorithm

  • step 1: Take the user input year and declare const.

  • step 2: check year is divisible by 400, 4 and not divisible by 100 then the year is a leap year if not then the year is not a leap year.

Javascript Code

const year = 2020;

if (year % 400 === 0 || (year % 4 === 0 && year % 100 !== 0)) {
  console.log(year + " is a Leap Year");
} else {
  console.log(year + " is not a Leap Year");
}

Output

2020 is a Leap Year

Method 3: Using Ternary Operator

Algorithm

  • step 1: Take the user input year and declare const.

  • step 2: create a flag variable

  • step 3: check year is divisible by 400, 4 and not divisible by 100 then the year is a leap year if not then the year is not a leap year using the ternary operator. store the result in a flag.

  • step 4: check flag is 1 then print leap year if not then year is not leap year.

Javascript Code

const year = 2019;

const flag = (year % 400 === 0) || (year % 4 === 0 && year % 100 !== 0) ? 1 : 0;
if (flag === 1) {
  console.log(year + " is a Leap Year");
} else {
  console.log(year + " is not a Leap Year");
}

Output

2019 is not a Leap Year

Method 4: Boolean method

Algorithm

  • step 1: Take the user input year and create a leap variable.

  • step 2: check year is divisible by 400 or not if yes then leap = true

  • step 3: if not then the year is divisible by 4 and year is not divisible by 100 then the leap year is true if not then the leap is false

  • step 4: check the leap variable value if the true print year is a leap year if false print year is not leap year.

Javascript Code

const year = 2016;
let leap;

if (year % 400 === 0) {
  leap = true;
} else if (year % 4 === 0 && year % 100 !== 0) {
  leap = true;
} else {
  leap = false;
}

if (leap) {
  console.log(year + " is a leap year.");
} else {
  console.log(year + " is not a leap year.");
}

Output

2016 is a leap year

Conclusion

So this article we solved the problem statement using javascript language. I hope you found this article helpful for your web development journey.

  • If you fill this article informative please like this article, if you have any suggestions please comment on this article, share it on social media and follow me on

  • Hashnode - Gaurav Patil

  • Twitter - @GauravYPatil