Friday, September 5, 2025
HomeLanguagesJavascriptJavaScript ReferenceError – Can’t access lexical declaration`variable’ before initialization

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 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 + "neveropen";
    }
}
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
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11866 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS