Skip to content

inRange

Checks whether a number falls within [min, max] (both inclusive by default).

Available since v2.0.0

import { inRange } from '@helpers4/number';
inRange(value: number, min: number, max: number, options: InRangeOptions): boolean
ParameterTypeDescription
valuenumberThe number to test
minnumberLower bound
maxnumberUpper bound
optionsInRangeOptionsBoundary inclusion mode (default: 'both')

boolean — true if value is within the specified range

Check if a value is within bounds (inclusive)

Section titled “Check if a value is within bounds (inclusive)”

Both min and max are included by default.

inRange(5, 1, 10)   // => true
inRange(0, 1, 10)   // => false
inRange(1, 1, 10)   // => true  (min included)
inRange(10, 1, 10)  // => true  (max included)

Use { inclusive: “none” } for open interval (min, max).

inRange(5, 1, 10, { inclusive: 'none' })  // => true
inRange(1, 1, 10, { inclusive: 'none' })  // => false
inRange(10, 1, 10, { inclusive: 'none' }) // => false

View source on GitHub