mrdoob/three.js · error
THREE.Vector4: index is out of range: ${index}
Error message
THREE.Vector4: index is out of range: ${index} What it means
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.
Source
Thrown at src/math/Vector4.js:225
}
/**
* Allows to set a vector component with an index.
*
* @param {number} index - The component index. `0` equals to x, `1` equals to y,
* `2` equals to z, `3` equals to w.
* @param {number} value - The value to set.
* @return {Vector4} A reference to this vector.
*/
setComponent( index, value ) {
switch ( index ) {
case 0: this.x = value; break;
case 1: this.y = value; break;
case 2: this.z = value; break;
case 3: this.w = value; break;
default: throw new Error( 'THREE.Vector4: index is out of range: ' + index );
}
return this;
}
/**
* Returns the value of the vector component which matches the given index.
*
* @param {number} index - The component index. `0` equals to x, `1` equals to y,
* `2` equals to z, `3` equals to w.
* @return {number} A vector component value.
*/
getComponent( index ) {
switch ( index ) {
View on GitHub (pinned to da05705fa3)
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).
Example fix
// before: looping past 4 for (let i = 0; i < 5; i++) v4.setComponent(i, data[i]); // i===4 throws // after: bound to 4 for (let i = 0; i < 4; i++) v4.setComponent(i, data[i]);
Defensive patterns
Strategy: validation
Validate before calling
function setComponentSafe(vec, index, value) {
if (index < 0 || index > 3) {
throw new RangeError(`Vector4 index must be 0-3, got ${index}`);
}
vec.setComponent(index, value);
return vec;
} Type guard
function isValidVector4Index(index) {
return Number.isInteger(index) && index >= 0 && index <= 3;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Generic loops with a wrong upper bound. Deserialization of tuples longer than 4. Index miscalculation in matrix/quaternion helpers.
Related errors
- THREE.Vector2: index is out of range: ${index}
- THREE.Vector3: index is out of range: ${index}
- THREE.CubeCamera.updateCoordinateSystem(): Invalid coordinat
- THREE.BufferAttribute: array should be a Typed Array.
- Unable to determine texture byte length for ${format} format
AI-assisted analysis of mrdoob/three.js@da05705fa3 (2026-08-12).
Data as JSON: /api/errors/14382c2c9419d724.
Report an issue: GitHub.