JavaScript Program to Check Whether Leap Year or Not

In this example, you will write a javascript program to check whether a year entered by the user is a leap year or not.

Condition for leap year:
  1. when the year is divisible by 400
  2. when the year is divisible by 4 and not divisible by 100.

Find leap year using if else


 function checkLeapYear() {
   // take input using a prompt method
   const year = prompt('Enter a year:');

   //conditions to find out the leap year
   if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
       document.write(year + ' is a leap year');
   } else {
       document.write(year + ' is not a leap year');
   }
}
checkLeapYear();

output

Enter a year: 2020
2020 is a leap year

find leap year using HTML and JavaScript


<!DOCTYPE html>
<html>
  <body>
<script>
 function checkLeapYear() {
	 // fetch the input
	 let year=document.getElementById("year").value;
	  
	 //conditions to find out the leap year
	 if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
		document.write(year + ' is a leap year');
	 }
	 else {
		document.write(year + ' is not a leap year');
	 }
 } 
</script>
  <form>
	 Enter year <input type="number" id="year" />
    <input type="button" value="submit" onclick="checkLeapYear()" />
  </form>
  </body>
</html>   
Run now

Take input from the user i.e document.getElementById("year").value and check if the year is satisfied with the above if condition then a year will be a leap year.