{"record":{"id":"897377d4f5bd4dfd","repo":"phaserjs/phaser","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":"src/utils/array/RemoveAt.js","lineNumber":32,"sourceCode":" * You can optionally specify a callback to be invoked for the item if it is successfully removed from the array.\n *\n * @function Phaser.Utils.Array.RemoveAt\n * @since 3.4.0\n *\n * @param {array} array - The array to be modified.\n * @param {number} index - The array index to remove the item from. The index must be in bounds or it will throw an error.\n * @param {function} [callback] - A callback to be invoked for the item removed from the array.\n * @param {object} [context] - The context in which the callback is invoked.\n *\n * @return {*} The item that was removed.\n */\nvar RemoveAt = function (array, index, callback, context)\n{\n    if (context === undefined) { context = array; }\n\n    if (index < 0 || index > array.length - 1)\n    {\n        throw new Error('Index out of bounds');\n    }\n\n    var item = SpliceOne(array, index);\n\n    if (callback)\n    {\n        callback.call(context, item);\n    }\n\n    return item;\n};\n\nmodule.exports = RemoveAt;\n","sourceCodeStart":14,"sourceCodeEnd":46,"githubUrl":"https://github.com/phaserjs/phaser/blob/41be1e462bc600064e498cba370bfa8c5c055a22/src/utils/array/RemoveAt.js#L14-L46","documentation":"Thrown by Phaser.Utils.Array.RemoveAt when the supplied index is negative or greater than the last valid index (array.length - 1). RemoveAt splices one element out by position and optionally fires a callback; the bounds check protects SpliceOne from a no-op or corrupting write. The check is strict on both ends: index 0 is valid, index length-1 is valid, anything outside throws.","triggerScenarios":"Calling RemoveAt(arr, -1), RemoveAt(arr, arr.length), or RemoveAt on an array whose length changed between computing the index and the call. Also RemoveAt([], 0) since length-1 is -1 and 0 > -1.","commonSituations":"Iterating an array backward with RemoveAt inside a loop and miscalculating the current index. Removing during a forEach/map where the index comes from a stale closure. Reading index from user input or a server response without bounds checking. Destroying a Phaser GameObject and then trying to RemoveAt its display-list index after the list shrank.","solutions":["Bounds-check first: `if (index >= 0 && index < arr.length) RemoveAt(arr, index);`.","When removing during iteration, iterate backward and recompute length each step.","Prefer RemoveAt(arr, arr.indexOf(item)) only after confirming indexOf !== -1.","For filtering by predicate, use Array.filter or Phaser.Utils.Array.RemoveEach instead of indexed removal."],"exampleFix":"// before\nfor (var i = 0; i < list.length; i++) {\n  if (shouldRemove(list[i])) Phaser.Utils.Array.RemoveAt(list, i);\n}\n\n// after\nfor (var i = list.length - 1; i >= 0; i--) {\n  if (shouldRemove(list[i])) Phaser.Utils.Array.RemoveAt(list, i);\n}","handlingStrategy":"validation","validationCode":"function safeRemoveAt(arr, index, cb, ctx) {\n  if (index >= 0 && index < arr.length) {\n    return Phaser.Utils.Array.RemoveAt(arr, index, cb, ctx);\n  }\n  return undefined;\n}","typeGuard":"function isInBounds(arr, index) {\n  return Number.isInteger(index) && index >= 0 && index < arr.length;\n}","tryCatchPattern":"try {\n  Phaser.Utils.Array.RemoveAt(arr, i);\n} catch (e) {\n  if (e.message === 'Index out of bounds') {\n    // list shrank; recompute and skip\n  } else { throw e; }\n}","preventionTips":["Bounds-check index against arr.length before calling.","Iterate backward when removing during a loop.","Recompute length each iteration when the array mutates.","Prefer predicate-based removal (Array.filter, Phaser.Utils.Array.RemoveEach) over indexed removal."],"tags":["array","remove","index","bounds","utils"],"backgroundTag":null,"analyzedSha":"41be1e462bc600064e498cba370bfa8c5c055a22","analyzedAt":"2026-08-13T04:23:39.729Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}