{"record":{"id":"bc4cdb6d43675e41","repo":"karatelabs/karate","slug":"cannot-convert-a-bigint-to-a-number-using-unary","errorCode":null,"errorMessage":"Cannot convert a BigInt to a number using unary +","messagePattern":"Cannot convert a BigInt to a number using unary \\+","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Interpreter.java","lineNumber":2457,"sourceCode":"    private static Object evalMathPreExpr(Node node, CoreContext context) {\n        Node exprNode = node.get(1).getFirst();\n        return switch (node.get(0).token.type) {\n            case PLUS_PLUS -> PropertyAccess.preIncDec(exprNode, context, true);\n            case MINUS_MINUS -> PropertyAccess.preIncDec(exprNode, context, false);\n            case MINUS -> {\n                Object v = eval(exprNode, context);\n                // Rare path: BigInt unary negation needs to stay in the BigInt domain.\n                // The plain-Number case below would TypeError because rhs (-1) is Integer.\n                if (v instanceof java.math.BigInteger bi) {\n                    yield Terms.narrowBigInt(bi.negate());\n                }\n                yield Terms.mul(v, -1, context);\n            }\n            case PLUS -> {\n                Object v = eval(exprNode, context);\n                // Spec: unary + on BigInt is a TypeError (no implicit BigInt → Number).\n                if (v instanceof java.math.BigInteger) {\n                    throw JsErrorException.typeError(\"Cannot convert a BigInt to a number using unary +\");\n                }\n                // Spec ToNumber routes through ToPrimitive (hint=number) for\n                // objects, so {valueOf: () => 7} returns 7 not NaN.\n                yield Terms.toNumberCoerce(v, context);\n            }\n            default -> throw new RuntimeException(\"unexpected operator: \" + node.getFirst());\n        };\n    }\n\n    private static Object evalProgram(Node node, CoreContext context) {\n        // A top-level \"use strict\" directive makes the whole script strict, and\n        // must be resolved before hoisting so function objects created here\n        // capture the right lexical strictness via their declaredContext.\n        if (!context.strict && hasUseStrictDirective(node)) {\n            context.strict = true;\n        }\n        // Program-scope slot frame: dense storage for let/const confined to\n        // nested blocks / for-inits at top level (SlotTable.forProgram — the","sourceCodeStart":2439,"sourceCodeEnd":2475,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Interpreter.java#L2439-L2475","documentation":"Thrown when unary `+` is applied to a BigInt value. Per the ECMAScript spec, unary plus performs ToNumber, and there is deliberately no implicit BigInt-to-Number conversion (it would lose precision), so `+bigint` is a TypeError.","triggerScenarios":"Evaluating `+bigintVar` in Karate JS — e.g. `+10n`, `+someBigIntResult` from arithmetic or JSON fields holding big integers.","commonSituations":"Code that coerces numbers with unary `+` (a common idiom for strings) encountering BigInt values produced by large-integer parsing or BigInt literals.","solutions":["Use `Number(bigint)` if an explicit (possibly lossy) conversion is intended.","Keep the value as BigInt and use BigInt arithmetic instead of coercing to number.","If string coercion was the goal, use `bigint.toString()`."],"exampleFix":"// before\nvar n = +bigValue;\n// after\nvar n = Number(bigValue); // explicit, or keep as BigInt","handlingStrategy":"type-guard","validationCode":"if (typeof v === 'bigint') throw new Error('unary + not allowed on BigInt');","typeGuard":"function isBigInt(v) { return typeof v === 'bigint' || v instanceof java.math.BigInteger; }","tryCatchPattern":"try { n = +value; } catch (e) { n = Number(value); }","preventionTips":["Use Number(x) explicitly for conversions","Avoid unary + in code that may see BigInt values","Keep BigInt math in BigInt domain"],"tags":["javascript","bigint","type-conversion"],"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"}