JavaScript switch greater than | Example code

  • by

You can use a JavaScript switch greater than the expression same as using in an if-else statement.

switch (true) {
  case (0 <= val &&  val < 1000): /* do something */ break;
  case (1000 <= val &&  val < 2000): /* do something */ break;
  ...
  case (29000 <= val &&  val < 30000): /* do something */ break;
}

JavaScript switch greater than

Simple example code compares the variable in the parenthesis with values either < 13 or >= 13.

<!DOCTYPE html>
<html>
<body>
  <script type="text/javascript">
    var age = 25;
    
    switch (true) {
      case (age < 13):
      alert("You must be 13 or older to play");
      break;
      case (age >= 13):
      alert("You are old enough to play");
      break;
    }
  </script>

</body>
</html>

Output:

JavaScript switch greater than

Do comment if you have any doubts or suggestions on this JS switch case topic.

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

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.