{"record":{"id":"8d07363a2673f243","repo":"karatelabs/karate","slug":"number-prototype-method-called-on-incompatible-receiver","errorCode":null,"errorMessage":"Number.prototype method called on incompatible receiver","messagePattern":"Number\\.prototype method called on incompatible receiver","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsNumberPrototype.java","lineNumber":85,"sourceCode":"                    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) {\n        Object thisObj = context.getThisObject();\n        if (thisObj instanceof JsNumber jn) return jn.value;\n        if (thisObj instanceof Number n) return n;\n        if (thisObj == INSTANCE) return 0;\n        throw JsErrorException.typeError(\"Number.prototype method called on incompatible receiver\");\n    }\n\n    /**\n     * ToInteger for digits/precision/fractionDigits args: dispatches through\n     * ToPrimitive so {@code [2]} → {@code \"2\"} → 2. Range-checking is the\n     * caller's job.\n     */\n    private static int toIntegerArg(Object arg, Context context) {\n        Number n = (context instanceof CoreContext cc)\n                ? Terms.toNumberCoerce(arg, cc)\n                : Terms.objectToNumber(arg);\n        double d = n.doubleValue();\n        if (Double.isNaN(d)) return 0;\n        return (int) d;\n    }\n\n    // Instance methods\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsNumberPrototype.java#L67-L103","documentation":"Number.prototype methods (toFixed, toPrecision, valueOf, toString, etc.) must be invoked with a Number as `this`. Karate's `thisNumber` accepts JsNumber, any java Number, or the prototype object itself; anything else (string, boolean, object, undefined) causes this TypeError, matching the spec's RequireInternalSlot check.","triggerScenarios":"`n.toFixed(2)` where n is a string like \"3.14\"; `Number.prototype.toFixed.call('3.14', 2)`; destructuring `const { toFixed } = (3.14)` is impossible but `const { toFixed } = Number.prototype; toFixed.call('1', 1)`; JSON-parsed values that stayed strings.","commonSituations":"Forgetting Number() coercion on JSON/text input before numeric formatting; Karate variables holding stringified numbers from config files or HTTP responses; strict-mode refactors that detached methods from receivers.","solutions":["Convert first: `Number(n).toFixed(2)` or the unary `+n`","Use `.call` with a real number: `Number.prototype.toFixed.call(+value, 2)`","Fix the data source so numbers are not serialized as strings (JSON parser settings, Karate `karate.get` coercion)","Validate `typeof value === 'number'` before calling numeric prototype methods"],"exampleFix":"// before\nconst v = karate.get('price'); // \"9.99\" (string)\nv.toFixed(2); // TypeError\n// after\nNumber(v).toFixed(2); // \"9.99\"","handlingStrategy":"type-guard","validationCode":"if (typeof value !== 'number') { throw new Error('expected a number, got ' + typeof value + ': ' + value); }","typeGuard":"function isNumber(x) { return typeof x === 'number' && !isNaN(x); }","tryCatchPattern":"try {\n  out = value.toFixed(2);\n} catch (e) {\n  if (String(e.message).includes('incompatible receiver')) {\n    out = Number(value).toFixed(2);\n  } else throw e;\n}","preventionTips":["Coerce JSON/text inputs with Number(x) or +x before numeric methods","Don't destructure Number.prototype methods without a number receiver","Verify Karate variables are numeric, not stringified, before formatting"],"tags":["javascript","number","receiver","typeerror"],"backgroundTag":"type-mismatch","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"}