Variable defined with a reserved word const can be changed - JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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' }


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 '

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo225.js.


The output is as follows −

PS C:\Users\Amit\JavaScript-code> node demo225.js
{ firstName: 'David', subjectDetails: { subjectName: 'JavaScript' } }
Updated on 03-Oct-2020 14:35:00