{"record":{"id":"a615975f78916431","repo":"karatelabs/karate","slug":"cannot-use-in-operator-to-search-for-lhs-in-rhs","errorCode":null,"errorMessage":"Cannot use 'in' operator to search for '${lhs}' in ${rhs}","messagePattern":"Cannot use 'in' operator to search for '(.+?)' in (.+?)","errorType":"exception","errorClass":"JsErrorException (typeError)","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Terms.java","lineNumber":1256,"sourceCode":"                if (current == target) {\n                    return true;\n                }\n                current = current.getPrototype();\n            }\n        }\n        return false;\n    }\n\n    /**\n     * ECMA relational {@code in} — returns {@code true} iff {@code rhs} (or its\n     * prototype chain) has a property named {@code ToPropertyKey(lhs)}.\n     * Throws {@link JsErrorException#typeError} when {@code rhs} is not an\n     * object (per spec §13.10.1 step 7), since the {@code [[HasProperty]]}\n     * internal method is only defined on objects.\n     */\n    static boolean in(Object lhs, Object rhs) {\n        if (!(rhs instanceof ObjectLike obj)) {\n            throw JsErrorException.typeError(\n                    \"Cannot use 'in' operator to search for '\"\n                            + String.valueOf(lhs) + \"' in \" + String.valueOf(rhs));\n        }\n        JsSymbol sym = JsSymbol.keyedBy(lhs);\n        if (sym != null) {\n            ObjectLike walk = obj;\n            while (walk != null) {\n                if (walk instanceof JsObject jo && jo.hasSymbol(sym)) {\n                    return true;\n                }\n                walk = walk.getPrototype();\n            }\n            return false;\n        }\n        String key = toPropertyKey(lhs);\n        ObjectLike current = obj;\n        while (current != null) {\n            if (current.isOwnProperty(key)) {","sourceCodeStart":1238,"sourceCodeEnd":1274,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Terms.java#L1238-L1274","documentation":"The JS `in` operator only works on objects (it checks [[HasProperty]]). Terms.in throws a TypeError when the right-hand side is a primitive (string, number, null, undefined, etc.), matching spec §13.10.1 step 7 and V8's message.","triggerScenarios":"Evaluating `'x' in 'abc'`, `0 in 5`, `'k' in null`, or `'k' in undefined` in karate-js scripts.","commonSituations":"Testing whether a map/key exists but passing a JSON-decoded scalar (strings and arrays from JSON responses are common culprits); confusing `in` with Java/Kotlin collection `contains`.","solutions":["Use the correct membership test for primitives: 'abc'.includes('a') for strings, arr.includes(x) for arrays","Ensure the right-hand side is an object: wrap scalars or use karate's map helpers","Guard before evaluating: if (typeof rhs === 'object' && rhs !== null) usesIn = 'k' in rhs"],"exampleFix":"// before\nif ('error' in response) { ... }\n// after (response may be a plain object or null)\nif (response && typeof response === 'object' && 'error' in response) { ... }","handlingStrategy":"type-guard","validationCode":"if (rhs === null || typeof rhs !== 'object') throw new Error(\"'in' requires an object rhs\");","typeGuard":"function canUseIn(v) { return v !== null && (typeof v === 'object' || typeof v === 'function'); }","tryCatchPattern":"try { has = 'k' in obj; } catch (e) { if (String(e).includes(\"'in' operator\")) has = false; else throw e; }","preventionTips":["Use .includes() for strings/arrays instead of `in`","Null-check values decoded from JSON before using `in`","Prefer `obj != null && key in obj` pattern in scripts"],"tags":["javascript","in-operator","type-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}