Thursday, September 4, 2025
HomeLanguagesJavascriptJavaScript ReferenceError – Assignment to undeclared variable

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 the error: Somewhere in the code, there is an assignment without the var, let, or const keywords. When a value is assigned to an undeclared variable this error occurs.

Example 1: In this example, the const keyword is used with the variable assignment, So the error has not occurred.

HTML




<!DOCTYPE HTML>
<html>
   
<body style="text-align:center;">
    <h1 style="color:green;">
        neveropen
    </h1>
    <p>
        JavaScript ReferenceError-
        Assignment to undeclared variable
    </p>
    <button onclick="Geeks();">
        click here
    </button>
    <p id="GFG_DOWN">
    </p>
 
    <script>
        let el_down = document.getElementById("GFG_DOWN");
        function GFG() {
            'use strict';
            const var_1 = "Value assigned without declaration";
        }
        function Geeks() {
            try {
                GFG();
                el_down.innerHTML =
                    "'Assignment to undeclared variable'" +
                    "error has not occurred";
            } catch (e) {
                el_down.innerHTML =
                    "'Assignment to undeclared variable'" +
                    "error has occurred";
            }
        }
    </script>
</body>
</html>


Output: 

Example 2: In this example, the var, let or const keyword is not used with the variable assignment, So the error has occurred.

HTML




<!DOCTYPE HTML>
<html>
   
<body style="text-align:center;">
    <h1 style="color:green;">
        neveropen
    </h1>
    <p>
        JavaScript ReferenceError -
        Assignment to undeclared variable
    </p>
    <button onclick="Geeks();">
        click here
    </button>
    <p id="GFG_DOWN">
    </p>
 
    <script>
        let el_down = document.getElementById("GFG_DOWN");
        function GFG() {
            'use strict';
            var_1 = true;
        }
        function Geeks() {
            try {
                GFG();
                el_down.innerHTML =
                    "'Assignment to undeclared variable'" +
                    "error has not occurred";
            } catch (e) {
                el_down.innerHTML =
                    "'Assignment to undeclared variable'" +
                    "error has occurred";
            }
        }
    </script>
</body>
</html>


Output: 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS