Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 18 Apr, 2021
Improve Article
Save Article

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.

prompt() Method:


prompt("Message to show");



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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!