You cannot change the value of an object when it is defined using const keyword.
After changing it will remain same.
Let’s say the following is our variable defined with const −
const details1 = { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } }
Example
Following is the code to change the const variable, which would only display the initial value −
const details1 = { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } } const details2 = { ...details1, subjectDetails: { ...details1.subjectDetails }, firstName: 'David' } details2.subjectDetails.subjectName = 'Java ' console.log(details1);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo225.js.
Output
The output is as follows −
PS C:\Users\Amit\JavaScript-code> node demo225.js { firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } }