DEV Community

Simc Dev
Simc Dev

Posted on • Originally published at cmsinstallation.blogspot.com

Const Object In Javascript

Understanding the Constant

Test 
    const USER = {
         'Name':'SIMC',
         'Age': 25
    }

    USER.Age = 27
    console.log(Age)

    What will be the output here ?

    Answer is 27
Enter fullscreen mode Exit fullscreen mode

Now you guys will think can we change the value of const ?

we have const as Object.....So as per rules we can change the properties of an object.

We can't change the object, we can only add or Edit

See Below Example

    const USER = {}
    USER = {'Age':'2'} 
Enter fullscreen mode Exit fullscreen mode

Will get error in above code...Why ? Because we are trying to reassign the const variable....

Conclusion

We can change the properties of an object which defined as a const. We cant reassign the object.

Found it useful ?

Top comments (2)

Collapse
 
brense profile image
Rense Bakker

Yep, let/const only control if you can reassign a variable or not. You should always use const when defining variables unless you have extremely good reasons not to. Const ensures that you do not have accidental reassignment of your variables from a higher scope, which results in unexpected behavior.

Collapse
 
devsimc profile image
Simc Dev

True