Why is it possible to change an array or object value from within a constant?

Asked

Viewed 356 times

15

const array = ["a", "b", "c", "d"];

array[1] = 2;

console.log(array); //- ['a',2,'c','d']

In this example I gave, I changed the value of the constant dynamically, too, it is possible to do the same with objects. I would like to know why this is possible, because theoretically the value of the constant could not be changed

  • 1

    If it is constant I say with the nerves. What would be the need to change a value that we will declare as const?

  • 1

    Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

3 answers

16

Because who is constant is the value placed there in the identifier and, contrary to what people think, the content of this identifier (which seems variable, but we cannot call it so because it does not vary) is a reference to the object. This value is constant, so you cannot point to another array in this identifier. But the object itself is not constant so you can mess with it however you want. It’s like you have an envelope with data inside and outside it, in this case you can’t touch the data outside, but you can touch what’s inside.

There are languages that can prohibit touching what is inside. In JS it is very limited, and in array there’s no way.

const array = ["a", "b", "c", "d"];
array[1] = 2;
console.log(array);
array = [1, 2]; //dá erro

I put in the Github for future reference.

15

As an old teacher of mine would say:

Don’t confuse "archipelago of Fernando de Noronha" with "skinny marijuana smoker". They may sound alike, but they’re completely different.

He said that basically in the sense that not everything is what it seems.

The term const in Javascript defines a reference constant to an object. That is, the reference cannot be overwritten. Once the reference points to an object, it cannot receive another object.

const number = 2;

number = 3;

Note that trying to overwrite the object in number gives error. However, being constant is different from being immutable. An immovable object cannot mutate and, "if it suffers", defines another object. For example, if I have the integer, which is immutable, 1, and I increment it into a unit I move to object 2, which is independent of original 1. Therefore, even assuming it as constant, I cannot modify it:

const number = 1;

number++;

The same error will be presented, because the object will be constant and immutable, different from a array He’s a changeable guy. Even giving it a constant reference, the object remains mutable, allowing it to mutate without representing another object:

const arr = [];

arr.push(1);

console.log(arr);

To MDN documentation comments on:

The statement const creates a read-only reference to a value. This does not mean that this value is immutable, only that the identifier of the constant variable cannot be changed. If the content of the identifier is an object, the parameters of that object may be changed, for example.

Other interesting readings:

If you prefer something more official:

7

You can change the properties of an object declared as const, What you can’t do is redeclare it.

You can either change the values of the declared array or const (or objects {}) how you can add new items:

const array = ["a","b","c","d"]
array[1] = 2
array.push("3")
console.log(array) //- ['a',2,'c','d','3']

The const does not define an unalterable value of a variable, it defines an unalterable reference for the variable. That is, once you declared the variable as const, this variable can no longer be redeclared, but its properties can be changed if it has them, as is the case with arrays, for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.