JavaScript Check if Number is Between Range

JavaScript Check if Number is Between Range | This is a straightforward but intriguing programming problem. Low, high, and x are three integers such that high >= low. How to use a single comparison to see if x is in the range [low, high]. For example, if the range is [30, 150] and the number is 43, the output is true; but, if the number is 987, the output is false.

Compare x with low and high values as a basic solution. In this article, we will see how to check if the number is between two numbers in javascript using two methods. But first, let us have a look at some simple examples to see what exactly we expect.

Example 1:
RangeAndNumber = [10, 50, 30]
Output:
30 is in the range of [10,50]

Example 2:
RangeAndNumber = [70, 100, 30]
Output:
30 is not in the range of [70,100]

To see if a number is between two numbers, use the following logic:

JavaScript Check if Number is Between Range Using Comparision

1. To chain two criteria, use the && (and) operator.
2. Check that the number is more than the lower range in the first condition and that it is less than the higher range in the latter.
3. The number is in the range when both conditions are met. 

const x = 35;

const low = 30;
const high = 150;

if (x > low && x < high) {
   console.log('It is in the given range');
} else {
   console.log('It is not in the given range');
}

Output:-

It is in the given range

Let us see another example for JS check if number is between range,

const x = 5;

const low = 30;
const high = 150;

if (x > low && x < high) {
   console.log('It is in the given range');
} else {
   console.log('It is not in the given range');
}

Output:-

It is not in the given range

To see if both conditions hold, we just use && (and) operator.

If these two requirements are met, the if block will run:

  •  The higher value exceeds the lower value.
  •  The lower value is greater than the higher value.

We know the number is in the required range if both conditions are met; otherwise, it isn’t.

From left to right, the && operator is evaluated. The function short-circuits and does not assess the second condition if the first clause in our if statement yields false.

Check if Value is Between Two Numbers JavaScript using Function

We can also use the inRange() method for Javascript check if number is between range.

function inRange(x, min, max) {
   return ((x - min) * (x - max) <= 0);
}

console.log(inRange(93, 10, 100)); 

Output:-

true

Let us see another example for JS check if number is between range,

function inRange(x, min, max) {
   return ((x - min) * (x - max) <= 0);
}

console.log(inRange(33, 45, 129.34)); 

Output:-

false

To check if a number is in the given range in javascript we can use the larger than or equal to and less than or similar to operators in JavaScript.

We can also perform the same thing with the inRange() method. We call inRange() with the number x that we want to check if it’s between 10 and 100. Also see:- Difference Between Switch And If Else In JavaScript

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.