Friday, September 5, 2025
HomeLanguagesJavascriptJavaScript ReferenceError – Invalid assignment left-hand side

JavaScript ReferenceError – Invalid assignment left-hand side

This JavaScript exception invalid assignment left-hand side occurs if there is a wrong assignment somewhere in code. A single “=” sign instead of “==” or “===” is an Invalid assignment.

Message:

ReferenceError: invalid assignment left-hand side

Error Type:

ReferenceError

Cause of the error: There may be a misunderstanding between the assignment operator and a comparison operator.

Basic Example of ReferenceError – Invalid assignment left-hand side, run the code and check the console

Example 1:

Javascript




if (Math.PI = 10 || Math.PI = 5) {
    console.log("Inside Loop");
}


Output:

ReferenceError: Invalid left-hand side in assignment

Example 1: In this example, “=” operator is misused as “==”, So the error occurred.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body style="text-align: center;">
    <h1 style="color: green;">
        neveropen
    </h1>
    <p>
        JavaScript ReferenceError -
        Invalid assignment left-hand side
    </p>
 
    <button onclick="Geeks();">
        click here
    </button>
    <p id="GFG_DOWN"></p>
 
    <script>
        let el_down = document.getElementById("GFG_DOWN");
        function Geeks() {
            try {
                if ((Math.PI = 10 || Math.PI = 5)) {
                    document.write("Inside Loop");
                }
                el_down.innerHTML =
                    "'Invalid assignment left-hand side'" +
                    " error has not occurred";
            } catch (e) {
                el_down.innerHTML =
                    "'Invalid assignment left-hand side'" +
                    "error has occurred";
            }
        }
    </script>
</body>
</html>


Output:

JavaScript ReferenceError - Invalid assignment left-hand side

Example 2: In this example, the + operator is used with the declaration, So the error has not occurred.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>Invalid assignment left-hand side</title>
</head>
<body style="text-align:center;">
    <h1 style="color:green;">
        neveropen
    </h1>
    <p>
        JavaScript ReferenceError -
        Invalid assignment left-hand side
    </p>
 
    <button onclick="Geeks();">
        click here
    </button>
    <p id="GFG_DOWN">
    </p>
 
    <script>
        let el_down = document.getElementById("GFG_DOWN");
        function Geeks() {
            try {
                let str = 'Hello, '
                    + 'Geeks'; // Error Here
                el_down.innerHTML =
                    "'Invalid assignment left-hand side'" +
                    "error has not occurred";
            } catch (e) {
                el_down.innerHTML =
                    "'Invalid assignment left-hand side'" +
                    "error has occurred";
            }
        }
    </script>
</body>
</html>


Output: 

JavaScript ReferenceError - Invalid assignment left-hand side

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
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