{"record":{"id":"2b47233f1554c664","repo":"karatelabs/karate","slug":"tostring-radix-must-be-between-2-and-36-jsnumberprototype","errorCode":null,"errorMessage":"toString() radix must be between 2 and 36","messagePattern":"toString\\(\\) radix must be between 2 and 36","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsNumberPrototype.java","lineNumber":62,"sourceCode":"        install(\"toPrecision\", 1, this::toPrecision);\n        install(\"toExponential\", 1, this::toExponential);\n        install(\"toLocaleString\", 0, this::toLocaleString);\n        install(\"toString\", 1, this::toStringMethod);\n        install(\"valueOf\", 0, this::valueOf);\n    }\n\n    private Object toStringMethod(Context context, Object[] args) {\n        Number n = thisNumber(context);\n        // Spec: only `undefined` (or absent) defaults to radix 10; `null` runs through\n        // ToInteger → 0 → RangeError.\n        if (args.length > 0 && args[0] != Terms.UNDEFINED) {\n            // ToIntegerOrInfinity rejects BigInt — spec mandates TypeError.\n            if (args[0] instanceof java.math.BigInteger) {\n                throw JsErrorException.typeError(\"Cannot convert a BigInt to a number\");\n            }\n            int radix = Terms.objectToNumber(args[0]).intValue();\n            if (radix < 2 || radix > 36) {\n                throw JsErrorException.rangeError(\"toString() radix must be between 2 and 36\");\n            }\n            if (radix != 10) {\n                double d = n.doubleValue();\n                if (d == Math.floor(d) && !Double.isInfinite(d)) {\n                    return Long.toString((long) d, radix);\n                }\n                return Double.toString(d);\n            }\n        }\n        return Terms.numberToString(n);\n    }\n\n    /**\n     * Spec {@code thisNumberValue} §21.1.3: unwrap JsNumber, accept primitive\n     * Number, TypeError otherwise. {@code Number.prototype} is a Number exotic\n     * with {@code [[NumberData]]} of +0, so it routes to 0.\n     */\n    private static Number thisNumber(Context context) {","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsNumberPrototype.java#L44-L80","documentation":"`Number.prototype.toString(radix)` requires the radix to be an integer between 2 and 36 inclusive. Karate throws this RangeError when ToIntegerOrInfinity of the radix falls outside that range (0, 1, negative, or > 36). Note `null`/`false` coerce to 0, so even they trigger it — only `undefined` or an absent argument defaults to 10.","triggerScenarios":"`(255).toString(1)`, `.toString(0)`, `.toString(37)`, `.toString(-2)`; passing `null` as the radix (null → ToInteger → 0 → RangeError); a radix variable computed out of bounds.","commonSituations":"Misunderstanding that null/false default the radix (they don't — only undefined does); algorithmically chosen bases (e.g. base-64 attempts); config-supplied radix values that are invalid.","solutions":["Use a radix in 2–36: `(255).toString(16)`","Omit the argument or pass `undefined` for decimal — do not pass null","Clamp/validate before calling: `radix >= 2 && radix <= 36 ? n.toString(radix) : n.toString()`"],"exampleFix":"// before\n(255).toString(null); // RangeError: null coerces to 0\n// after\n(255).toString(); // decimal, or (255).toString(10)","handlingStrategy":"validation","validationCode":"if (radix !== undefined && !(Number.isInteger(radix) && radix >= 2 && radix <= 36)) { throw new Error('radix must be an integer 2-36'); }","typeGuard":"function validRadix(r) { return r === undefined || (typeof r === 'number' && Number.isInteger(r) && r >= 2 && r <= 36); }","tryCatchPattern":"try {\n  s = n.toString(radix);\n} catch (e) {\n  if (String(e.message).includes('radix must be between 2 and 36')) {\n    s = n.toString(); // fall back to decimal\n  } else throw e;\n}","preventionTips":["Omit the argument for decimal; don't pass null (coerces to 0)","Clamp radix to [2, 36] before use","Remember only undefined defaults to base 10 — not null/false/0"],"tags":["javascript","number","rangeerror","radix"],"backgroundTag":"value-out-of-range","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}