Open In App

JavaScript ReferenceError – Can’t access lexical declaration`variable’ before initialization

Last Updated : 05 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This JavaScript exception can’t access the lexical declaration `variable’ before initialization occurs if a lexical variable has been accessed before initialization. This could happen inside any block statement when let or const declarations are accessed when they are undefined.

Message: 

ReferenceError: Use before declaration (Edge)
ReferenceError: can't access lexical declaration `variable' before 
                initialization (Firefox)
ReferenceError: 'variable' is not defined (Chrome)

Error Type: 

ReferenceError

Cause of the error: Somewhere in the code, there is a lexical variable that was accessed before initialization.

Example 1: In this example, the const keyword is used with the variable inside the if statement, So the error has occurred.

Javascript




function GFG() {
    const var_1 = "This is";
    if (true) {
        const var_1 = var_1 + "GeeksforGeeks";
    }
}
function Geeks() {
    try {
        GFG();
        console.log(
            "'Can't access lexical declaration" +
            "`variable'before initialization' " +
            "error has not occurred");
    } catch (e) {
        console.log(
            "'Can't access lexical declaration" +
            "`variable' before initialization'" +
            " error has occurred");
    }
}
Geeks()


Output

'Can't access lexical declaration`variable' before initialization' error has occurred

Example 2: In this example, the keyword is used with the variable, So the error has occurred.

Javascript




function GFG() {
    let var_1 = 3;
    if (true) {
        var_1 = var_1 + 5;
    }
}
function Geeks() {
    try {
        GFG();
        console.log(
            "'Can't access lexical declaration" +
            "`variable' before initialization'" +
            " error has not occurred");
    } catch (e) {
        console.log(
            "'Can't access lexical declaration" +
            "`variable'before initialization'" +
            " error has occurred");
    }
}
Geeks()


Output

'Can't access lexical declaration`variable' before initialization' error has not occurred


Previous Article
Next Article

Similar Reads

JavaScript ReferenceError - Assignment to undeclared variable
This JavaScript exception Assignment to undeclared variable occurs in strict-mode If the value has been assigned to an undeclared variable. Message: ReferenceError: assignment to undeclared variable "x" (Firefox) ReferenceError: "x" is not defined (Chrome) ReferenceError: Variable undefined in strict mode (Edge) Error Type: ReferenceError Cause of
2 min read
JavaScript ReferenceError - variable is not defined
This JavaScript exception variable is not defined and occurs if there is a non-existent variable that is referenced somewhere. Message: ReferenceError: "x" is not defined Error Type: ReferenceError Cause of Error: There is a non-existent variable that is referenced somewhere in the script. That variable has to be declared or make sure the variable
1 min read
JavaScript ReferenceError - Reference to undefined property "x"
This JavaScript warning reference to undefined property occurs if a script tries to access an object property that doesn't exist. Message: ReferenceError: reference to undefined property "x" (Firefox) Error Type: ReferenceError(Only reported by firefox browser) Cause of the error: The script is trying to access an object property that doesn't exist
2 min read
JavaScript ReferenceError - Invalid assignment left-hand side
This JavaScript exception invalid assignment left-hand side occurs if there is a wrong assignment somewhere in code. A single “=” sign instead of “==” or “===” is an Invalid assignment. Message: ReferenceError: invalid assignment left-hand side Error Type: ReferenceError Cause of the error: There may be a misunderstanding between the assignment ope
2 min read
JavaScript ReferenceError Deprecated caller or arguments usage
This JavaScript exception deprecated caller or arguments usage occurs only in strict mode. It occurs if any of the Function.caller or Function.arguments properties are used, Which are depreciated. Message: TypeError: 'arguments', 'callee' and 'caller' are restricted function properties and cannot be accessed in this context (Edge) Warning: Referenc
1 min read
JavaScript SyntaxError - A declaration in the head of a for-of loop can't have an initializer
This JavaScript exception a declaration in the head of a for-of loop can't have an initializer occurs if the for -of loop contains an initialization expression like |for (var i = 0 of iterable)|. This is not valid initialization in for-of loops. Message: SyntaxError: for-of loop head declarations cannot have an initializer (Edge) SyntaxError: a dec
1 min read
Uncaught ReferenceError: controller is not defined
Uncaught ReferenceError: the controller is not defined is a common error message that can occur in JavaScript. It usually occurs when you try to access a variable or function that has not been defined or is out of scope. An example where this error occurs: For example, the following code will result in an Uncaught ReferenceError: the controller is
1 min read
Uncaught ReferenceError: $ is not a function
In this article, we will see the "Uncaught ReferenceError: $ is not a function". The ReferenceError is an object that represents an error that arises when the variable is not initialized or simply, variable doesn't exist in the current scope & it is referenced. The $ sign is an alias of jQuery. The $ is not defined ReferenceError usually arises
2 min read
How to catch an "ReferenceError" with "onerror" ?
The "ReferenceError" is a very common error that can occur when programming in JavaScript. The "onerror" event is a great way to catch these errors and prevent them from causing your program to crash. What is a "ReferenceError"? A "ReferenceError" occurs when your program tries to access a variable or object that does not exist. This can happen whe
2 min read
Uncaught ReferenceError at HTMLButtonElement.onclick
In this article article, let's first discuss the Reference error and then come to onclick function and then we come over the question Uncaught ReferenceError at HTMLButtonElement.onclick using example and lastly come to the solution approach. Onclick Function in javaScript: The onclick event handlers are a group of properties offered by DOM element
2 min read