Wednesday, September 3, 2025
HomeLanguagesJavascriptJavaScript TypeError – Invalid assignment to const “X”

JavaScript TypeError – Invalid assignment to const “X”

This JavaScript exception invalid assignment to const occurs if a user tries to change a constant value. Const declarations in JavaScript can not be re-assigned or re-declared.

Message:

TypeError: invalid assignment to const "x" (Firefox)
TypeError: Assignment to constant variable. (Chrome)
TypeError: Assignment to const (Edge)
TypeError: Redeclaration of const 'x' (IE)

Error Type:

TypeError

Cause of Error: A const value in JavaScript is changed by the program which can not be altered during normal execution. 

Example 1: In this example, the value of the variable(‘GFG’) is changed, So the error has occurred.

HTML




<script>
    const GFG = "This is GFG";
    // Error here
    GFG = "This is GeeksForGeeks"; 
</script>


Output(in console):

TypeError: Assignment to const

Example 2: In this example, the value of the object(‘GFG_Obj’) is changed, So the error has occurred.

HTML




<script>
    const GFG_Obj = {key: 'val1'};
    // Error here
    GFG_Obj = {key: 'val2'}; 
</script>


Output:

TypeError: Assignment to const
RELATED ARTICLES

Most Popular

Dominic
32260 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6625 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6746 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6694 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS