{"record":{"id":"4d9bdb84ec4c0cb0","repo":"karatelabs/karate","slug":"cannot-access-key-before-initialization","errorCode":null,"errorMessage":"cannot access '${key}' before initialization","messagePattern":"cannot access '(.+?)' before initialization","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/CoreContext.java","lineNumber":338,"sourceCode":"\n    Object get(String key) {\n        if (\"this\".equals(key)) {\n            return thisObject;\n        }\n        if (callArgs != null && \"arguments\".equals(key)) {\n            JsArray a = argumentsForRead();\n            if (a != null) {\n                return a;\n            }\n            // arrow frame with no enclosing function — ordinary resolution\n        }\n        if (frameTable != null) {\n            int idx = frameTable.indexOf(key);\n            if (idx >= 0) {\n                Object v = frame[idx];\n                if (v != SlotTable.UNDECLARED) {\n                    if (v == SlotTable.TDZ) {\n                        throw JsErrorException.referenceError(\"cannot access '\" + key + \"' before initialization\");\n                    }\n                    return v;\n                }\n                // undeclared: fall through — pre-declaration the store lacks\n                // the name too, so the legacy chain is the same resolution\n            }\n        }\n        BindingSlot s = resolve(key);\n        if (s == null) {\n            return Terms.UNDEFINED;\n        }\n        return readSlot(s, key);\n    }\n\n    /** The `arguments` visible at this frame: the frame's own for a normal\n     *  function call, the lexically enclosing function's for an arrow frame\n     *  (§10.2.1.3 — arrows have no own `arguments`; identity is shared with\n     *  the enclosing frame's object). Returns null for an arrow with no","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/CoreContext.java#L320-L356","documentation":"This is a temporal-dead-zone (TDZ) ReferenceError: a `let`/`const` binding exists in the frame table but still holds the TDZ sentinel, meaning the declaration statement has not executed yet. Per the JS spec, reading such a binding must throw rather than return undefined.","triggerScenarios":"CoreContext.get(key) resolving a name whose frame slot equals SlotTable.TDZ — e.g. referencing a let/const variable above its declaration, or from a closure/handler invoked before initialization runs.","commonSituations":"Hoisting assumptions from `var` carried over to `let`/`const`; callbacks (setTimeout, event handlers) firing before the declaration line executes; use of a variable inside a function defined before the let/const statement.","solutions":["Move the declaration above the first use of the variable.","Rename the variable if an outer/global with the same name was intended.","Convert to `var` only if legacy hoisting semantics are truly wanted (discouraged).","Ensure deferred callbacks don't run before the declaration executes."],"exampleFix":"// before\nconsole.log(x); let x = 1;\n// after\nlet x = 1; console.log(x);","handlingStrategy":"try-catch","validationCode":"// ensure declaration executed before access\nif (typeof x !== 'undefined') { /* safe only for var/global; TDZ still throws */ }","typeGuard":null,"tryCatchPattern":"try { use(x); } catch (e) { if (e instanceof ReferenceError && e.message.includes('before initialization')) { /* declare x first or reorder */ } else throw e; }","preventionTips":["Declare let/const at the top of their scope.","Don't rely on var-style hoisting for let/const.","Keep deferred callbacks from firing before declarations execute."],"tags":["javascript","tdz","reference-error"],"backgroundTag":"access-before-initialization","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"}