Skip to content
Related Articles

Related Articles

Improve 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.

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!"


Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :