karatelabs/karate · error · JsErrorException

Invalid array length

Error message

Invalid array length

What it means

When the Array constructor is called with a single numeric argument (Array(n)), the number must be a valid, non-negative, integral, unsigned-32-bit array length. NaN, Infinity, negative, non-integer, or values above 4294967295 produce this RangeError, mirroring the ECMAScript spec.

Solutions

  1. Validate the length before constructing: ensure it is a non-negative integer <= 4294967295
  2. Use Math.floor/Math.trunc on computed lengths, and guard against NaN
  3. If you want an array with that element, pass it as a second argument or use literal syntax instead

Example fix

// before
var n = getSize(); var arr = new Array(n); // RangeError if n invalid
// after
var n = Math.trunc(getSize()); if (n >= 0 && n <= 4294967295) { var arr = new Array(n); }
Defensive patterns

Strategy: validation

Validate before calling

function isValidArrayLength(n) { return typeof n === 'number' && Number.isInteger(n) && n >= 0 && n <= 4294967295; }

Type guard

function isValidArrayLength(n) { return typeof n === 'number' && Number.isInteger(n) && n >= 0 && n <= 4294967295; }

Try / catch

try { var arr = new Array(n); } catch (e) { if (String(e).indexOf('Invalid array length') !== -1) { arr = []; } else { throw e; } }

Prevention

When it happens

Trigger: new Array(-1), new Array(2.5), new Array(NaN), new Array(Infinity), new Array(4294967296).

Common situations: Computing a length from a parsed value or variable that ends up negative/NaN; off-by-one or unit-conversion arithmetic producing fractional lengths; porting code that relied on coerced lengths.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/eaa9db0ae8efd63e. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsArray.java:1044

    @Override
    public Object call(Context context, Object[] args) {
        return create(args);
    }

    /**
     * ES6: Both Array() and new Array() return an Array object.
     * Array(n) creates a sparse array of length n (HOLE-filled, per spec —
     * {@code new Array(3).hasOwnProperty(0) === false}); Array(a,b,c) creates
     * [a,b,c]. Throws RangeError when the single-numeric form's argument is
     * not a valid Uint32 — covers {@code new Array(-1)} /
     * {@code new Array(4294967296)} / {@code new Array(1.5)}.
     */
    static JsArray create(Object[] args) {
        if (args.length == 1 && args[0] instanceof Number n) {
            double d = n.doubleValue();
            if (Double.isNaN(d) || Double.isInfinite(d) || d < 0
                    || d > 4294967295.0 || d != Math.floor(d)) {
                throw JsErrorException.rangeError("Invalid array length");
            }
            if (d > Integer.MAX_VALUE) {
                throw JsErrorException.rangeError("Invalid array length");
            }
            int count = (int) d;
            List<Object> list = new ArrayList<>(count);
            for (int i = 0; i < count; i++) {
                list.add(HOLE);
            }
            return new JsArray(list);
        }
        return new JsArray(new ArrayList<>(Arrays.asList(args)));
    }

    // Use identity-based hashCode/equals to avoid infinite recursion
    // when arrays contain objects with circular references
    @Override
    public int hashCode() {

View on GitHub (pinned to a22eb90246)