Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Example:

HTML




<div>
    <h1 style="color:green">GeeksforGeeks</h1>
  
    <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>


Output:

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

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


Last Updated : 15 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads