Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to display a message when given number is between the range using JavaScript ?

  • Last Updated : 18 Apr, 2021

In this article, we will learn to display a message when a number is between the given range using JavaScript. We take a number from the user and tell the user whether it is in between a range or not, in our case, the range is 1 to 10.  

Approach: We create a button and add a click event listener to it. When we click on button, a prompt is shown on the browser screen using the JavaScript prompt() function asking for input. The variable is checked if it’s in between the range or not by using the JavaScript if-else statement. If this input is in between the range, we show a success message on the screen using the innerHTML property otherwise, we show a failure message.

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!

prompt() Method:

Syntax:

prompt("Message to show");

Example:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <style>
        .gfg1 {
            font-size: 30px;
            color: green;
        }
  
        #gfg2 {
            font-size: 30px;
            color: green;
        }
  
        div {
            margin-left: 30%;
        }
  
        button {
            font-size: 20px;
        }
    </style>
</head>
  
<body>
    <div>
        <p class="gfg1">GeeksforGeeks</p>
  
        <button onclick="fun()">
            click me
        </button>
          
        <p id="gfg2"></p>
    </div>
  
    <script>
        function fun() {
            const input = prompt('Please enter a number:');
  
            if (input >= 1 && input <= 10)
                document.getElementById("gfg2")
                    .innerHTML = input + " Success!";
            else
                document.getElementById("gfg2")
                    .innerHTML = input + " Fail!"
        }
    </script>
</body>
  
</html>

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!