-2

As here shows

// allowed
const helloWorld = {
  text: 'Welcome to the Road to learn React'
};
helloWorld.text = 'Bye Bye React';

works.

But why helloworld, as a constant object, its member can be modified? what is essentially const mean in react javascript?

Tuhin
  • 3,335
  • 2
  • 16
  • 27
athos
  • 6,120
  • 5
  • 51
  • 95

2 Answers2

3

The const declaration creates a read-only reference to a value.

when you are assigning an object to a const. you are creating a reference to that object.

const helloWorld = {
   text: 'Welcome to the Road to learn React'
};

Now const helloWorld does not know about the implementation or inner structure what it is holding. Javascript just has told to "helloWorld" that "I am giving you this object, hold it. and Listen!!! man!!!,, you are not supposed to hold anything else".

So, helloWorld does the same. it holds the same object. But people can change the inner parts of the object.

Hence,

People can do following:

helloWorld.text = 'Bye Bye React';

Here helloWorld is still holding the same object. hellowWorld will allow you to do anything with what it is holding. But, what it won't do is: "it won't drop it"

So, people can not do following:

helloWorld = "something else";

People cannot ask to helloWorld to hold something else.

Tuhin
  • 3,335
  • 2
  • 16
  • 27
1

Const will define a object no reassignable but we can edit properties in the object. Using const won't change the structure of the object.