{"record":{"id":"c9e822a0c322ead8","repo":"TheAlgorithms/JavaScript","slug":"out-of-range-index","errorCode":null,"errorMessage":"Out of Range index","messagePattern":"Out of Range index","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"Data-Structures/Linked-List/SinglyLinkedList.js","lineNumber":163,"sourceCode":"\n  // Returns the index of the element passed as param otherwise -1\n  indexOf(element) {\n    if (this.isEmpty()) return -1\n    let { currentNode, currentIndex } = this.initiateNodeAndIndex()\n    while (currentNode) {\n      if (currentNode.data === element) {\n        return currentIndex\n      }\n      currentNode = currentNode.next\n      currentIndex++\n    }\n    return -1\n  }\n\n  // Returns the element at an index\n  elementAt(index) {\n    if (index >= this.length || index < 0) {\n      throw new RangeError('Out of Range index')\n    }\n    let { currentIndex, currentNode } = this.initiateNodeAndIndex()\n    while (currentIndex < index) {\n      currentIndex++\n      currentNode = currentNode.next\n    }\n    return currentNode.data\n  }\n\n  // Adds the element at specified index\n  addAt(index, element) {\n    // Check if index is out of bounds of list\n    if (index > this.length || index < 0) {\n      throw new RangeError('Out of Range index')\n    }\n    if (index === 0) return this.addFirst(element)\n    if (index === this.length) return this.addLast(element)\n    let { currentIndex, currentNode } = this.initiateNodeAndIndex()","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Data-Structures/Linked-List/SinglyLinkedList.js#L145-L181","documentation":"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.","triggerScenarios":"Calling elementAt(list.length) (treating length as a valid slot); calling elementAt(0) on an empty list (length 0); calling elementAt(-1).","commonSituations":"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.","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."],"exampleFix":"// before\nconst v = list.elementAt(i) // throws when i === list.length\n\n// after\nconst v = (i >= 0 && i < list.length) ? list.elementAt(i) : undefined","handlingStrategy":"validation","validationCode":"function safeElementAt(list, index) {\n  if (index < 0 || index >= list.length) return undefined\n  return list.elementAt(index)\n}","typeGuard":"const isValidReadIndex = (list, i) =>\n  Number.isInteger(i) && i >= 0 && i < list.length","tryCatchPattern":"try {\n  return list.elementAt(index)\n} catch (e) {\n  if (e instanceof RangeError && /out of range/i.test(e.message)) return undefined\n  throw e\n}","preventionTips":["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."],"tags":["data-structures","linked-list","range-error","index-bounds"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}