JavaScript ReferenceError - Can't access lexical declaration`variable' before initialization - GeeksforGeeks

29 Jul, 2021
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.
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: 
surindertarika1234
JavaScript-Errors
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
How to filter object array based on attributes?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?