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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output: 

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

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output: 

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.