EN
JavaScript - check if value is in range
0 points
In this article, we would like to show you how to check if a value is in range using JavaScript.
Quick solution:
xxxxxxxxxx
1
const number = 2;
2
3
const min = 1; // range min
4
const max = 3; // range max
5
6
console.log(number >= min && number <= max); // true
In the example below, we create a reusable function that takes 3 arguments:
number
- the number we want to check,min
- range minimum value,max
- range maximum value.
xxxxxxxxxx
1
const isInRange = (number, min, max) => {
2
return number >= min && number <= max;
3
};
4
5
6
// Usage example:
7
8
// check if 2 is between 1 and 3
9
console.log(isInRange(2, 1, 3));
10
11
// check if 5 is between 1 and 3
12
console.log(isInRange(5, 1, 3));