javascriptnode.jsobjectconstantsfindbugs

how to constant object or remain object values same in javascript?


Here is basic example where I want to explain my requirement . I want not to change object a values after assigning it to const b and changing its values .I want console result original object a values not after assign values . how can I achive this in console name :abc, number 123

const a ={
name:"abc",
number:123
}
const b = a
b.name ="xyz";
b.number = 321
 console.log(a);


Solution

  • simply do this with b so that b will not reference to a

    const b = {...a}