TheAlgorithms/JavaScript · error · RangeError
Out of Range index
Error message
Out of Range index
What it means
Thrown by SinglyLinkedList.elementAt(index) as a RangeError when index is negative or >= length. Unlike insertion, read access is only valid on the half-open range [0, length); there is no valid 'past-the-end' element, so index === length is rejected.
Source
Thrown at Data-Structures/Linked-List/SinglyLinkedList.js:163
// Returns the index of the element passed as param otherwise -1
indexOf(element) {
if (this.isEmpty()) return -1
let { currentNode, currentIndex } = this.initiateNodeAndIndex()
while (currentNode) {
if (currentNode.data === element) {
return currentIndex
}
currentNode = currentNode.next
currentIndex++
}
return -1
}
// Returns the element at an index
elementAt(index) {
if (index >= this.length || index < 0) {
throw new RangeError('Out of Range index')
}
let { currentIndex, currentNode } = this.initiateNodeAndIndex()
while (currentIndex < index) {
currentIndex++
currentNode = currentNode.next
}
return currentNode.data
}
// Adds the element at specified index
addAt(index, element) {
// Check if index is out of bounds of list
if (index > this.length || index < 0) {
throw new RangeError('Out of Range index')
}
if (index === 0) return this.addFirst(element)
if (index === this.length) return this.addLast(element)
let { currentIndex, currentNode } = this.initiateNodeAndIndex()View on GitHub (pinned to 5c39e87a9a)
Solutions
- Bound the index with 0 <= index < list.length before reading.
- For empty-list safety, check list.isEmpty() (or length === 0) first and short-circuit.
- When iterating, use for (let i = 0; i < list.length; i++) so length is never used as an index.
- If you need a default instead of a throw, wrap elementAt in a helper returning undefined for out-of-range.
Example fix
// before const v = list.elementAt(i) // throws when i === list.length // after const v = (i >= 0 && i < list.length) ? list.elementAt(i) : undefined
Defensive patterns
Strategy: validation
Validate before calling
function safeElementAt(list, index) {
if (index < 0 || index >= list.length) return undefined
return list.elementAt(index)
} Type guard
const isValidReadIndex = (list, i) => Number.isInteger(i) && i >= 0 && i < list.length
Try / catch
try {
return list.elementAt(index)
} catch (e) {
if (e instanceof RangeError && /out of range/i.test(e.message)) return undefined
throw e
} Prevention
- Use strict < in loop bounds: for (let i = 0; i < list.length; i++).
- Check isEmpty() before any elementAt on a possibly-empty list.
- Remember elementAt is half-open [0, length); length itself is invalid.
- Wrap elementAt in a helper returning undefined for out-of-range if that suits your domain.
When it happens
Trigger: Calling elementAt(list.length) (treating length as a valid slot); calling elementAt(0) on an empty list (length 0); calling elementAt(-1).
Common situations: Iterating with <= instead of < in a loop bound; using the size returned by one method as an index into another; reading from a list that was concurrently cleared.
Related errors
- Index is out of range max ${this.length}
- Index Out of Bound
- Queue is Empty
- Stack Underflow
- Unsupported base. Must be in range [2, 10]
AI-assisted analysis of TheAlgorithms/JavaScript@5c39e87a9a (2026-08-13).
Data as JSON: /api/errors/c9e822a0c322ead8.
Report an issue: GitHub.