{"record":{"id":"14382c2c9419d724","repo":"mrdoob/three.js","slug":"three-vector4-index-is-out-of-range-index","errorCode":null,"errorMessage":"THREE.Vector4: index is out of range: ${index}","messagePattern":"THREE\\.Vector4: index is out of range: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/math/Vector4.js","lineNumber":225,"sourceCode":"\t}\n\n\t/**\n\t * Allows to set a vector component with an index.\n\t *\n\t * @param {number} index - The component index. `0` equals to x, `1` equals to y,\n\t * `2` equals to z, `3` equals to w.\n\t * @param {number} value - The value to set.\n\t * @return {Vector4} A reference to this vector.\n\t */\n\tsetComponent( index, value ) {\n\n\t\tswitch ( index ) {\n\n\t\t\tcase 0: this.x = value; break;\n\t\t\tcase 1: this.y = value; break;\n\t\t\tcase 2: this.z = value; break;\n\t\t\tcase 3: this.w = value; break;\n\t\t\tdefault: throw new Error( 'THREE.Vector4: index is out of range: ' + index );\n\n\t\t}\n\n\t\treturn this;\n\n\t}\n\n\t/**\n\t * Returns the value of the vector component which matches the given index.\n\t *\n\t * @param {number} index - The component index. `0` equals to x, `1` equals to y,\n\t * `2` equals to z, `3` equals to w.\n\t * @return {number} A vector component value.\n\t */\n\tgetComponent( index ) {\n\n\t\tswitch ( index ) {\n","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/mrdoob/three.js/blob/da05705fa31f02710c19772a2aa70c7454807f0c/src/math/Vector4.js#L207-L243","documentation":"Thrown by Vector4.setComponent(index, value) when index is not 0, 1, 2, or 3. setComponent maps 0->x, 1->y, 2->z, 3->w; a Vector4 has exactly four components, so index 4 and above (or negatives) are out of range.","triggerScenarios":"Calling vec4.setComponent(4, v) or any index outside [0,3]. Looping with a dimension > 4. Passing an index from a higher-rank structure. Negative or NaN index.","commonSituations":"Generic loops with a wrong upper bound. Deserialization of tuples longer than 4. Index miscalculation in matrix/quaternion helpers.","solutions":["Keep the index within [0,3] for Vector4; clamp or validate first.","Confirm your source data actually has only four values.","Set fields directly (vec.x/y/z/w) when the index is constant.","Size loops from the actual vector dimension (4)."],"exampleFix":"// before: looping past 4\nfor (let i = 0; i < 5; i++) v4.setComponent(i, data[i]); // i===4 throws\n\n// after: bound to 4\nfor (let i = 0; i < 4; i++) v4.setComponent(i, data[i]);","handlingStrategy":"validation","validationCode":"function setComponentSafe(vec, index, value) {\n  if (index < 0 || index > 3) {\n    throw new RangeError(`Vector4 index must be 0-3, got ${index}`);\n  }\n  vec.setComponent(index, value);\n  return vec;\n}","typeGuard":"function isValidVector4Index(index) {\n  return Number.isInteger(index) && index >= 0 && index <= 3;\n}","tryCatchPattern":null,"preventionTips":["Bound indexed loops at the target vector's size (4 for Vector4).","Confirm the source data has at most four channels.","Prefer direct field access (vec.x/y/z/w) for known components."],"tags":["threejs","vector","vector4","index","bounds"],"backgroundTag":null,"analyzedSha":"da05705fa31f02710c19772a2aa70c7454807f0c","analyzedAt":"2026-08-12T22:51:37.160Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}