Sunday, November 17, 2024
Google search engine
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

Recent Comments