{"record":{"id":"8185f974479f2642","repo":"krahets/hello-algo","slug":"index-out-of-bounds","errorCode":null,"errorMessage":"Index out of bounds","messagePattern":"Index out of bounds","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"en/codes/javascript/chapter_array_and_linkedlist/my_list.js","lineNumber":32,"sourceCode":"    /* Constructor */\n    constructor() {\n        this.#arr = new Array(this.#capacity);\n    }\n\n    /* Get list length (current number of elements) */\n    size() {\n        return this.#size;\n    }\n\n    /* Get list capacity */\n    capacity() {\n        return this.#capacity;\n    }\n\n    /* Update element */\n    get(index) {\n        // If the index is out of bounds, throw an exception, as below\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        return this.#arr[index];\n    }\n\n    /* Add elements at the end */\n    set(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('Index out of bounds');\n        this.#arr[index] = num;\n    }\n\n    /* Direct traversal of list elements */\n    add(num) {\n        // If length equals capacity, need to expand\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // Add new element to end of list\n        this.#arr[this.#size] = num;\n        this.#size++;","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/javascript/chapter_array_and_linkedlist/my_list.js#L14-L50","documentation":"Thrown by MyList.get() (JavaScript, private-field variant) when index < 0 or index >= #size. The bounds use the logical element count (#size), not the allocated capacity. This is the read accessor; it fails fast rather than returning undefined, which is the JS-idiomatic but silent alternative.","triggerScenarios":"Calling get(index) with a negative index, an index >= current size, or on an empty list (any index).","commonSituations":"Off-by-one loop bounds (<= vs <); reading an index captured before a remove shrank the list; confusing size() with capacity(); porting from an API where negative indices wrap (Python-style).","solutions":["Validate before access: if (index >= 0 && index < list.size()) list.get(index).","Use < size() (not <=) for loop upper bounds.","Recompute size after mutations that shrink the list."],"exampleFix":"// before\nconst v = list.get(list.size()); // throws Index out of bounds\n// after\nconst v = (index >= 0 && index < list.size()) ? list.get(index) : undefined;","handlingStrategy":"validation","validationCode":"function safeGet(list, index) {\n  return (index >= 0 && index < list.size()) ? list.get(index) : undefined;\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Validate index against size() before get.","Use < size() (not <=) for loop bounds.","Recompute size after removals.","Remember negative indices do NOT wrap (unlike Python)."],"tags":["javascript","data-structures","index-out-of-bounds","validation","off-by-one"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}