erradicate

How to fix TypeError: Assignment to constant variable

Technologies
webnode.js
Variants
  • TypeError: invalid assignment to const 'foo' (Firefox)

Example

const foo = 'bar';
foo = 'qux'; // TypeError: Assignment to constant variable

const obj = {};
obj = { foo: 'bar' }; // TypeError: Assignment to constant variable

Solution

Use let instead of const if the variable will be reassigned later.

let foo = 'bar';
foo = 'qux'; // no error

let obj = {};
obj = { foo: 'bar' }; // no error