Check if a Number is Between Two Numbers in JavaScript

Borislav Hadzhiev

Last updated: Mar 2, 2024

Reading timeยท6 min

- Check if a Number is Between Two Numbers
- Check if a Number is between two numbers using Math.max() and Math.min()
- Check if a Number is between two numbers using the ternary operator
- Check if a Number is between two numbers using lodash

**To check if a number is between two numbers:**

- Check if the number is greater than the lower range.
- Check if the number is less than the higher range.
- If both conditions are met, the number is between the two numbers.

index.js

`const num = 50; const low = 30; const high = 150; if (num > low && num < high) { // ๐๏ธ this runs console.log('โ num is between the two numbers'); } else { console.log('โ๏ธ num is NOT between the two numbers'); }`

The code for this article is available on GitHub

If you aren't sure which of the two numbers is the lower number which is the
greater number, use `Math.max()`

and `Math.min()`

instead.

index.js

`// โ If you aren't sure which of the two numbers is low // and which is high use Math.max() and Math.min() const num = 50; const low = 30; const high = 150; const max = Math.max(low, high); console.log(max); // ๐๏ธ 150 const min = Math.min(low, high); console.log(min); // ๐๏ธ 30 if (num > min && num < max) { // ๐๏ธ this runs console.log('โ num is between the two numbers'); } else { console.log('โ๏ธ num is NOT between the two numbers'); }`

We used the logical AND (&&) operator to check if multiple conditions are true.

The `if`

block only runs if the following two conditions are met:

- the number is greater than the lower value
- the number is less than the higher value

index.js

`const num = 50; const low = 30; const high = 150; if (num > low && num < high) { // ๐๏ธ this runs console.log('โ num is between the two numbers'); } else { console.log('โ๏ธ num is NOT between the two numbers'); }`

If both conditions are met, the number is in the specified range, otherwise, it isn't.

The logical AND

`&&`

operator is evaluated from left to right. If the first condition in our `if`

statement returns `false`

, the operator short-circuits and doesn't evaluate the second condition.If you need to make the `low`

and `high`

ranges inclusive, use the `>=`

and `<=`

operators.

index.js

`const num = 30; const low = 30; const high = 150; if (num >= low && num <= high) { // ๐๏ธ this runs console.log('โ num is between the two numbers'); } else { console.log('โ๏ธ num is NOT between the two numbers'); }`

The code for this article is available on GitHub

The code sample checks if the number is greater than or equal to `low`

and less
than or equal to `high`

.

If you have to check if a number is between two numbers often, define a reusable function.

index.js

`function numberInRange(num, low, high) { if (num > low && num < high) { return true; } return false; } console.log(numberInRange(5, 1, 10)); // ๐๏ธ true console.log(numberInRange(50, 1, 10)); // ๐๏ธ false`

The `numberInRange()`

function takes 3 numbers as parameters and checks if the
first number is between the other two numbers.

If you aren't sure which of the two numbers is `low`

and which is `high`

, use
the `Math.max()`

and `Math.min()`

functions.

`Math.max()`

and `Math.min()`

This is a three-step process:

- Use the
`Math.max()`

function to get the higher of the two numbers. - Use the
`Math.min()`

function to get the lower of the two numbers. - Check if the number is higher than the lower range and less than the higher range.

index.js

`const num = 50; const low = 30; const high = 150; const max = Math.max(low, high); console.log(max); // ๐๏ธ 150 const min = Math.min(low, high); console.log(min); // ๐๏ธ 30 if (num > min && num < max) { // ๐๏ธ this runs console.log('โ num is between the two numbers'); } else { console.log('โ๏ธ num is NOT between the two numbers'); }`

The code for this article is available on GitHub

This approach is useful when you don't know which of the two numbers is the higher number and which is the lower.

We used the `Math.max()`

method to get the higher of the two numbers and the
`Math.min()`

method to get the lower number.

index.js

`const first = 30; const second = 150; const max = Math.max(first, second); console.log(max); // ๐๏ธ 150 const min = Math.min(first, second); console.log(min); // ๐๏ธ 30`

Once we have the lower and higher ranges, we can use an `if`

statement to check
if the number is less than the higher range and greater than the lower range.

If both conditions are met, the number is between the two numbers.

If you have to do this often, define a reusable function.

index.js

`function numberInRange(num, first, second) { const max = Math.max(first, second); const min = Math.min(first, second); if (num > min && num < max) { return true; } return false; } console.log(numberInRange(5, 10, 50)); // ๐๏ธ false console.log(numberInRange(5, 1, 50)); // ๐๏ธ true console.log(numberInRange(5, 50, 1)); // ๐๏ธ true`

The function takes a number and two other numbers that aren't bound to a
specific order and returns `true`

if the number is between the two numbers and
`false`

otherwise.

If you want to include the lower and higher range, change the conditions to check for less than or equal to and greater than or equal to.

index.js

`const num = 30; const low = 30; const high = 150; if (num >= low && num <= high) { // ๐๏ธ this runs console.log('โ num is between the two numbers'); } else { console.log('โ๏ธ num is NOT between the two numbers'); }`

The code for this article is available on GitHub

The `if`

statement checks if the number is greater than or equal to the lower
range and less than or equal to the higher range.

The logical AND `&&`

operator is evaluated from left to right. If the first
condition in our `if`

statement returns `false`

, the operator short-circuits and
doesn't evaluate the second condition.

For example, in the following `if`

statement, the `5 > 1`

condition is never
evaluated.

index.js

`if (1 > 100 && 5 > 1) { }`

We first check if `1 > 100`

and get a value of `false`

, so the `&&`

operator
short-circuits.

The `if`

block is only run if both conditions are met and the number is in the
specified range.

You can also use the ternary operator to check if a number is between two numbers.

index.js

`const num = 50; const low = 30; const high = 150; const inRange = num > low && num < high ? true : false; if (inRange) { // ๐๏ธ this runs console.log('The number is in the specified range'); } else { console.log('The number is NOT in the specified range'); }`

The ternary operator is
very similar to an `if/else`

statement.

If the expression to the left of the question mark is truthy, the operator returns the value to the left of the colon, otherwise, the value to the right of the colon is returned.

You can imagine that the value before the colon is the `if`

block and the value
after the colon is the `else`

block.

We first check if the `num`

variable is greater than `low`

and then check if it
is lower than `high`

.

If the condition is met, we return `true`

, otherwise, `false`

is returned.

`lodash`

If you use the `lodash`

library, you can also check if a number is in a certain
range by using the lodash.inRange
method.

index.js

`import _ from 'lodash'; const num = 50; const low = 30; const high = 150; console.log(_.inRange(num, low, high)); // ๐๏ธ true if (_.inRange(num, low, high)) { // ๐๏ธ this runs console.log('The number is in the range'); } else { console.log('The number is NOT in the range'); }`

The code for this article is available on GitHub

If you need to install the `lodash`

library, run the following command.

If you need to install `lodash`

, run the following command.

shell

`# ๐๏ธ initialize a package.json file npm init -y npm install lodash`

The `lodash.inRange`

method takes the following parameters:

`number`

- the number to check.`start`

- the start of the range (inclusive).`end`

- the end of the range (up to, but not including).

The `lodash.inRange`

method checks if a number is between `start`

and up to, but
not including `end`

.

index.js

`console.log(_.inRange(50, 0, 100)); // ๐๏ธ true console.log(_.inRange(300, 0, 100)); // ๐๏ธ false console.log(_.inRange(0, -100, 50)); // ๐๏ธ true`

If `end`

is not specified, it is set to `start`

and `start`

is then set to `0`

.

If `start`

is greater than `end`

, the parameters are swapped to support negative
ranges.

You can learn more about the related topics by checking out the following tutorials: