Skip to content

isWithinRange

Jason Godesky edited this page Jul 11, 2026 · 1 revision

isWithinRange returns true if the number n is within the defined range or false if it is outside of it.

Parameters

n: number

The number to test.

range: number[]

An array of numbers to define the range. The highest value in the array is the maximum and the lowest value is the minimum.

inclusive: boolean = true

Should this be an inclusive comparison (meaning that if n is equal to the minimum or maximum, it is counted as within range)? If set to false, then an exclusive comparison is applied instead (n must be strictly greater than the minimum and less than the maximum).

Default: true

Returns

true if n is within the range or false if it is not.

Examples

import { isWithinRange } from '@revolutionarygamesco/common'

isWithinRange(0, [1, 10]) // false
isWithinRange(1, [1, 10]) // true
isWithinRange(1, [1, 10], false) // false, because you asked for an exclusive comparison
isWithinRange(5, [1, 10]) // true
isWithinRange(5, [1, 10]) // true even when exclusive
isWithinRange(10, [1, 10]) // true
isWithinRange(10, [1, 10], false) // false, because you asked for an exclusive comparison
isWithinRange(11, [1, 10]) // false

Clone this wiki locally