JavaScript program to check if a given year is leap year

Prerequisite:Understanding basic JavaScript codes.
A year is leap year if following conditions are satisfied:

  • Year is multiple of 400.
  • Year is multiple of 4 and not multiple of 100.

Approach:

  • Get the value of input field by using document.getElementById(“year”).value
  • Check the given year is leap year or not by using JavaScript expression and display its value.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!-- HTML code to implement the given year is 
    leap year or not -->
<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript to check leap year
    </title>
</head>
  
<body>
    Input Year: <input type="text" id = "year"/>
      
    <input type="button" id="button" onClick="isLeapYear()"
            value="Check Leap Year">
      
    <p id="GFG"></p>
      
    <!-- JavaScript code to check given year is
        leap year or not -->
    <script>
        function isLeapYear() {
            var year= document.getElementById("year").value;
              
            document.getElementById("GFG").innerHTML 
                = (year % 100 === 0) ? (year % 400 === 0)
                                     : (year % 4 === 0);
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:

  • Before input year:
  • After input leap year:
  • After input not leap year:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.