Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

JavaScript program to check if a given year is leap year

  • Difficulty Level : Basic
  • Last Updated : 12 Apr, 2019

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:

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • 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:




<!-- 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>                    

Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!