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

10

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, as theoretically the value of the constant could not be changed

    
asked by anonymous 29.09.2018 / 01:17

3 answers

13

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 can not call it that because it does not vary) is a reference for the object. This value is constant, so it can not point to another array in this identifier. But the object itself is not constant so you can move it as you wish. It's like having an envelope with data inside and outside it, in this case you can not tinker with the data from outside, but you can tweak what's inside.

It has languages that can forbid to tinker with what is inside. In JS it is very limited, and in array it has no way.

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

As one old teacher of mine would say:

  

Do not confuse "arquipelago de Fernando de Noronha" with "skinny smoking marijuana" . They may sound alike, but they are completely different.

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

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

const number = 2;

number = 3;

Note that attempting to overwrite the object with number gives error. But being constant is different from being unchangeable. An immutable object can not mutate, and if it does, it defines another object. For example, if I have the integer, which is immutable, 1, and increment it in a unit I will have object 2, which is independent of the original 1. Therefore, even assuming it as constant, I can not modify it:

const number = 1;

number++;

The same error will be displayed because the object will be constant and immutable, unlike an array that is a changeable type. Even by assigning it a constant reference, the object continues to be mutable, allowing it to mutate without representing another object:

const arr = [];

arr.push(1);

console.log(arr);

MDN documentation comments on:

  

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

Other interesting readings:

If you prefer something more official:

29.09.2018 / 01:44
6

You can change the properties of an object declared as const , which you can not redeclare.

You can either change the values of the declared array as const (or {} objects) as 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']

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

    
29.09.2018 / 01:36