Skip to content

If else condition in HTML code | Example

  • by

You can not use the If else condition in HTML code (Without JavaScript). Because HTML is a markup language and not a programming language. You need to do logic codes either server-side or with Javascript.

If else condition in HTML code

Simple example code.

<!DOCTYPE html>
<html>
<head>
  <script>
    var age=20;
    var ans;
    
    if (age < 18) {
      ans = "Underage"
    } else {
      ans = "Let em in!";
    }
    
  </script>
</head>

<body>

  <script>
    document.write(ans)
  </script>

</body>
</html>

Output:

If else condition in HTML code

Do comment if you have any doubts or suggestions on this HTML code.

Note: The All HTML Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading