{"record":{"id":"95a5ae4e5393efc7","repo":"TheAlgorithms/JavaScript","slug":"index-is-out-of-range-max-this-length","errorCode":null,"errorMessage":"Index is out of range max ${this.length}","messagePattern":"Index is out of range max (.+?)","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"Data-Structures/Linked-List/SinglyCircularLinkedList.js","lineNumber":62,"sourceCode":"  add(data) {\n    if (!this.headNode) {\n      return this.addAtFirst(data)\n    }\n    const node = new Node(data)\n    // Getting the last node\n    const currentNode = this.getElementAt(this.length - 1)\n    currentNode.next = node\n    node.next = this.headNode\n    this.length++\n    return this.length\n  }\n\n  // insert data at a specific position\n  insertAt(index, data) {\n    if (index === 0) return this.addAtFirst(data)\n    if (index === this.length) return this.add(data)\n    if (index < 0 || index > this.length)\n      throw new RangeError(`Index is out of range max ${this.length}`)\n    const node = new Node(data)\n    const previousNode = this.getElementAt(index - 1)\n    node.next = previousNode.next\n    previousNode.next = node\n    this.length++\n    return this.length\n  }\n\n  // find the first index of the data\n  indexOf(data) {\n    let { currentNode } = this.initiateNodeAndIndex()\n    // initializing currentIndex as -1\n    let currentIndex = -1\n    while (currentNode) {\n      if (currentNode.data === data) {\n        return currentIndex + 1\n      }\n      currentIndex++","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Data-Structures/Linked-List/SinglyCircularLinkedList.js#L44-L80","documentation":"Thrown by SinglyCircularLinkedList.insertAt(index, data) as a RangeError when the requested index is negative or strictly greater than the list's current length. This library treats insertion as valid over the closed range [0, length] (index 0 delegates to addAtFirst, index === length delegates to add), so anything outside that band is rejected rather than silently clamped.","triggerScenarios":"Calling list.insertAt(-1, x); calling list.insertAt(5, x) on a list whose length is 2; calling insertAt on an empty list with any index other than 0.","commonSituations":"Off-by-one after deletions shrink length; passing a 1-based index into a 0-based API; recomputing the insertion point from a stale size captured before a clear/remove.","solutions":["Compute the index in 0-based terms and clamp to [0, list.length] before calling insertAt.","Guard the call: if (index < 0 || index > list.length) handle the invalid case yourself.","Re-read list.size() immediately before computing the index so it reflects any prior mutations.","For append-style use, prefer add(data) (end) or addAtFirst(data) (head) which never throw on bounds."],"exampleFix":"// before\nlist.insertAt(userIndex, value) // throws if userIndex is stale/negative\n\n// after\nconst safeIndex = Math.max(0, Math.min(userIndex, list.length))\nif (safeIndex !== userIndex) throw new RangeError(`clamped ${userIndex}`)\nlist.insertAt(safeIndex, value)","handlingStrategy":"validation","validationCode":"function safeInsertAt(list, index, data) {\n  if (!Number.isInteger(index) || index < 0 || index > list.length) {\n    throw new RangeError(`index must be an integer in [0, ${list.length}]`)\n  }\n  return list.insertAt(index, data)\n}","typeGuard":"const isValidInsertIndex = (list, i) =>\n  Number.isInteger(i) && i >= 0 && i <= list.length","tryCatchPattern":"try {\n  list.insertAt(index, data)\n} catch (e) {\n  if (e instanceof RangeError && /out of range/i.test(e.message)) {\n    // handle bad index (e.g. clamp or report)\n  } else throw e\n}","preventionTips":["Always treat the list as 0-based; valid insertion range is the closed interval [0, length].","Re-read list.size() immediately before computing an index if the list may have changed.","Prefer add() / addAtFirst() for end/head insertion to avoid index math entirely.","Never pipe indexOf()'s -1 sentinel into insertAt without remapping it."],"tags":["data-structures","linked-list","range-error","index-bounds","circular-list"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}