JavaScript

Check if a Number is Between Two Numbers in JavaScript

While performing mathematical computations in JavaScript, there can be a requirement to compare different numbers based on some particular attribute. For instance, sorting some data based on age. In such cases, checking if a number is between two numbers in JavaScript is of great aid in determining the equality or difference between the values.

This write-up will demonstrate the approaches to check if a number is between two numbers in JavaScript.

How to Check if a Number is Between Two Numbers Using JavaScript?

To check if a number is between two numbers using JavaScript, the following approaches can be utilized:

  • &&” comparison operator.
  • Ternary” operator.
  • Math.min()” and “Math.max()” methods.

Let’s demonstrate the stated approaches one by one!

Approach 1: Check if a Number is Between Two Numbers in JavaScript Using && Comparison Operator

The “&&” operator evaluates a result based on all stated conditions. This operator can be utilized to apply a condition upon the specified number to check if it is between the other two specified numbers or not.

Example
Let’s follow the below-given example:

<script type="text/javascript">
let numBet = 50;
let min = 40;
let max = 60;
if (numBet > min && numBet < max) {
 console.log('The number is between the two numbers');
}
else {
 console.log('The number is not between the two numbers');
}
</script>

Implement the following steps in the above code snippet:

  • Firstly, specify a number that needs to be checked for the required condition.
  • After that, initialize the stated numbers to be compared with the number in the previous step.
  • In the next step, apply the “if/else” condition and the “&&” operator to check whether the specified number is between the minimum and maximum numbers, respectively.
  • Upon the satisfied condition, the “if” condition will execute. In the other scenario, the “else” condition will come into effect.

Output

In the above output, it can be observed that the specified number is in between the two stated numbers.

Approach 2: Check if a Number is Between Two Numbers in JavaScript Using Ternary Operator

The “ternary operator” is a conditional operator having the same functionality as “if/else”. This operator can be applied likewise to execute a condition upon the function arguments and return the corresponding output with the help of the “&&” operator and “template literals”.

Syntax

condition ? <expression> : <expression>

In the above syntax:

  • The former expression represents the “true” expression
  • The latter expression refers to the “false” expression.

Example
Let’s move on to the following example:

<script type="text/javascript">
function numBet(minNum, betNum, maxNum){
 (betNum > minNum && betNum < maxNum) ?
  console.log(`The number ${betNum} is between ${minNum} and ${maxNum}`)
  : console.log("The number is not between the two numbers");
}
numBet(10,20,30);
</script>

In the above lines of code:

  • Define a function named “numBet()” having the stated parameters for comparing the numbers.
  • In its definition, likewise, apply a condition upon a particular passed number for checking if it is between the other two numbers with the help of the “&&” operator.
  • The former statement will execute upon the satisfied condition with the help of the “template literals”.
  • In the other scenario, the latter statement will be displayed.

Output

The above output signifies that the number passed as an argument is between the other two passed numbers.

Approach 3: Check/Verify if a Number is in Between Two Numbers in JavaScript Using Math.min() and Math.max() Methods

The “Math.min()” method gives the number having the lowest value, and the “Math.max()” method gives the number having the highest value. These methods can be implemented to input a number from the user and compare it with the minimum and maximum of the passed numbers with the help of a user-defined function.

Syntax

Math.min(num1, num2,...)
Math.max(num1, num2,...)

In the above syntax:

  • num1” and “num2” refer to the numbers to be compared for the minimum and maximum values, respectively.

Example
Let’s go through the below-given example:

<script type="text/javascript">
let get = prompt("Enter a number")
function numBet(a, b) {
 var min = Math.min(a, b),
 max = Math.max(a, b);
 if (get>min && get<max){
  console.log("The number is between the two numbers")
  }
 else{
  console.log("The number is not between the two numbers")
}
};
console.log(numBet(500,600));
</script>

Apply the following steps as given in the above code:

  • Firstly, input a number from the user using the “prompt” box.
  • In the next step, define a function named “numBet()” having the stated parameters.
  • In the function definition, get the minimum and maximum of the passed numbers as function arguments.
  • After that, recall the discussed approach for applying a condition upon the stated numbers with the help of the “&&” operator.
  • Finally, access the defined function having the passed arguments to be compared with the user input value.

Output

In the above output, both of the conditions work properly upon the user-defined number.

Conclusion

The “&&” comparison operator, the “ternary” operator, or the “Math.min()” and “Math.max()” methods can be used to check if a number is in between two numbers using JavaScript. The && operator can be utilized simply to compare a particular number with the other two specified numbers. The ternary operator also performs the same operation. Whereas Math.min() and Math.max() can be implemented to compute the minimum and maximum of the parameters and compare them with the user-defined number. This tutorial explained to verify if a number is in between two numbers using JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.