Skip to content
Related Articles

Related Articles

JavaScript ReferenceError – Can’t access lexical declaration`variable’ before initialization
  • Last Updated : 24 Jul, 2020

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 delaration (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.

HTML




<!DOCTYPE html>
<html>
    <head>
    </head>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
          
        <p>
          JavaScript ReferenceError - 
          Can't access lexical declaration`variable' 
          before initialization
        </p>
  
        <button onclick="Geeks();">
            click here
        </button>
        <p id="GFG_DOWN"></p>
  
        <script>
            var el_down = document.getElementById("GFG_DOWN");
            function GFG() {
                const var_1 = "This is";
                if (true) {
                    const var_1 = var_1 + "GeeksforGeeks";
                }
            }
            function Geeks() {
                try {
                    GFG();
                    el_down.innerHTML = 
                      "'Can't access lexical declaration"+
                      "`variable'before initialization' "+
                      "error has not occurred";
                } catch (e) {
                    el_down.innerHTML = 
                      "'Can't access lexical declaration"+
                      "`variable' before initialization'"+
                      " error has occurred";
                }
            }
        </script>
    </body>
</html>

Output: 

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

HTML




<!DOCTYPE html>
<html>
    <head>
    </head>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
          
        <p>
          JavaScript ReferenceError -
          Can't access lexical declaration`variable' 
          before initialization
        </p>
  
        <button onclick="Geeks();">
            click here
        </button>
        <p id="GFG_DOWN"></p>
  
        <script>
            var el_down = document.getElementById("GFG_DOWN");
            function GFG() {
                let var_1 = 3;
                if (true) {
                     var_1 = var_1 + 5;
                }
            }
            function Geeks() {
                try {
                    GFG();
                    el_down.innerHTML = 
                      "'Can't access lexical declaration"+
                      "`variable' before initialization'"+
                      " error has not occurred";
                } catch (e) {
                    el_down.innerHTML = 
                      "'Can't access lexical declaration"+
                      "`variable'before initialization'"+
                      " error has occurred";
                }
            }
        </script>
    </body>
</html>

Output: 

My Personal Notes arrow_drop_up
Recommended Articles
Page :