change a constant object in JavaScript

 

the following ways through an error 

const x = {};

x = {foo: 'bar'}; // error - re-assigning

const y = ['foo'];
const y = ['bar']; // error - re-declaring

const foo = 'bar'; 
foo = 'bar2';       // error - can not re-assign
var foo = 'bar3';   // error - already declared
function foo() {};  // error - already declared

 

Right syntax

 

const x = {foo: 'bar'};

x["foo"] = "zoo";


Tags:

Share:

Related posts