Skip to content

isWithinBounds

Jason Godesky edited this page Aug 18, 2026 · 1 revision

isWithinBounds returns true if the number n is a valid index for the given array arr, or false if it would be out of bounds.

Parameters

n: number

The potential index to test.

arr: Array<any>

The array to test against.

Returns

true if n is a valid index for arr, or false if it is not.

Examples

import { isWithinBounds } from '@revolutionarygamesco/common'

const arr = [1, 2, 3]

isWithinBounds(-1, arr) // false
isWithinBounds(0, arr) // true
isWithinBounds(2, arr) // true
isWithinBounds(3, arr) // false
isWithinBounds(1000, arr) // false

Clone this wiki locally