HOWTO · JavaScript

How to Check if Number Is Between Two Values in JavaScript

In this article, we will see how to check if a number falls between two values in JavaScript.

On this page

To check whether a number is between two values in JavaScript, compare it with both bounds and join the comparisons with logical AND (&&). For example, number > lower && number < upper checks whether number is strictly inside the range. This version excludes values equal to either bound. If the range should include both endpoints, use >= and <= instead.

Check Whether a Number Is Between Two Values in JavaScript

A range check combines two comparisons: the number must be greater than the lower bound and less than the upper bound. The && operator makes the whole condition true only when both comparisons are true. If either comparison is false, the number is outside the strict range.

For example, with bounds 10 and 50, the value 25 passes the strict check. The values 10 and 50 do not, because > and < exclude equality. To include the endpoints, write number >= lower && number <= upper. Choose the operators to match the rule your program needs; “between” does not by itself specify whether endpoints count.

JavaScript comparison operators produce Boolean results for numeric operands. See MDN’s comparison operators documentation and logical operators documentation for details. The examples below assume that the value and both bounds are numbers, with the lower bound less than the upper bound.

Use an if Statement to Test a Number Range

A named predicate keeps the range rule reusable. This example uses an if statement to choose between two messages. Run this example:

function isBetween(number, lower, upper) {
  return number > lower && number < upper;
}

const value = 25;
if (isBetween(value, 10, 50)) {
  console.log(`${value} is between 10 and 50`);
} else {
  console.log(`${value} is outside the range`);
}

The function returns a Boolean, so you can use it anywhere a condition is expected. For the example value, the first comparison is 25 > 10 and the second is 25 < 50; both are true, so the if branch runs. If either comparison is false, control goes to else. The block form is useful when either branch needs several statements or when naming each branch makes the logic easier to follow.

Use the Ternary Operator to Test a Number Range

For a short choice between two values, the same predicate can be used in a ternary expression. Its condition comes before ?; the expression between ? and : is selected when the condition is true, and the expression after : is selected when it is false.

function isBetween(number, lower, upper) {
  return number > lower && number < upper;
}

const value = 25;
const message = isBetween(value, 10, 50)
  ? `${value} is between 10 and 50`
  : `${value} is outside the range`;
console.log(message);

This has the same strict endpoint behavior as the if example because both use isBetween. A ternary expression produces a value, which can be assigned or passed to another expression. Keep each branch brief; a long nested ternary makes the condition and its outcomes harder to scan.

Choose Between if and the Ternary Operator

Use if when a branch performs multiple actions, contains more complex logic, or benefits from explicit control flow. Use a ternary expression when you are selecting one of two concise values and the result remains easy to read. Both forms rely on the same range predicate, so changing the predicate’s comparisons changes the endpoint rule in either place.

Keep the bounds in the intended order. If a function might receive bounds in either order, normalize them before checking, for example by assigning the smaller value to lower and the larger to upper. Also ensure the inputs are numbers before comparing them: JavaScript’s comparison operators can coerce values of other types, which may not match the validation rule your application intends. For user-entered data, parse and validate the value and bounds first, then apply the range predicate.