Why is it possible to change constant values in Javascript arrays?

Asked

Viewed 190 times

2

Having the following code:

const arr = [1,2,3];
console.log(arr);
arr.push(4);
console.log(arr);
arr.pop();
console.log(arr);

arr shouldn’t it be constant? And therefore shouldn’t accept the method push() and pop()? After all, if I include or exclude an element of this array I’m changing its value, so it’s not constant. Or I’m getting the concept of constant in JS wrong?

  • I didn’t understand the closing, or the closing acceptance since the linked question doesn’t say anything about array.

  • A constant only avoids the superscription/redeclaration of the variable or its value. This does not mean that this value is immutable because only the identifier of the variable that cannot be changed. If the content of the identifier is an object or an array, the parameters/values of that object can be changed normally.

2 answers

1


You need to understand the difference between types by value and types by reference. In types by value the content (value) is in the variable, in types by reference the content (value) is in another location, and the variable has only one pointer to that location.

The const refers to this value of what would be a variable (in this case it remains a constant). So the code cannot change the pointer stored there, that is, you cannot take that identifier and point to an object other than the one initially pointed to.

The content of array is not constant, that is, what you have on the storage site can be changed as much as you want.

Currently, as far as I know, there’s no way to make the elements of arrays are constant (there are tricks to achieve indirectly similar semantics).

You can even declare a variable and point to the same object. And being a variable, it can point to another object whenever it wants, detaching itself from this object that already has a constant reference.

Therefore constancy (immutability would be a more appropriate term, JS, like many languages, does not press for the most correct terminology) is external to the object. Some languages allow internal "constancy".

Do not confuse the value associated with the identifier to the identifier itself. The identifier exists in the code, it is a word. The value exists in the execution and is where the code determines as specified. The identifier may not even, after all it does not exist, or does not matter, during execution (in Javascript this is not true for all identifiers, but only for cases where the identifier is at bottom a value, and if changed create huge complicators, in practice it should not be done).

0

I think that, in a more practical way, the explanation is as follows::

When you say the word reserved const creates a "variable" of type constante, that is, that such a variable will not be changed because it has become invariable or immutable, in fact, it was not the whole variable that became a constante. Let’s see: roughly, the variables (I speak in the case of Javascript) can be composed of three elements/characteristics: 1) Identifier (or reference); 2) Type; 3) value. So, let’s look at the examples below:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

In the first crop, a variable was created with the reserved word var, which makes it a variable that can be changed at any time. Beauty. In the second cut, we have the statement using the reserved word const, to say what to parser Javascript? Here’s the question: The JS parser, when finding the term const puts an "immutability" flag not in the [Value] element of constante, but in the [Identifier] element. That is, declare a variable as constante, does not mean, objectively, that your valor will be constant in all cases, and yes that your identificação and consequently its tipo, will always be immutable. How is this true? Simple: See the three examples below (run them):

const a = "Por que não posso mudar?"
const a = "Porque o tipo dessa constante é [String], e para alterar o tipo, precisaria REdeclarar a constante, o que é proibido!

const a = [1,2,3];
console.log(a);

a[0] = 99;
console.log(a);

const carro = {marca:"Volvo", modelo:"XC40"};
console.log(carro);

carro["marca"]="Land Rover";
carro["modelo"]="Range Rover Velar";
console.log(carro);

Check it out! the value of the constants of examples 2 and 3 were successfully changed! And then I ask: a constante is it really ALWAYS constant? It is proven not. The valor, depending on the type, can be changed. Specifically, if a constante is the type Arrayor Object, then, the elements of that Array or the members of that Object can be changed, no problem, as this does not imply in the Redeclaration of constante - what we did was declare values for types of constantes that allow this attitude.

inserir a descrição da imagem aqui

The flagof which I spoke (which the Javascript parser puts in a variable when finding the term const) is applied to the element [Identifier], which by table (and logic), does not also allow modifying the element [Type], since I can only change the type, redeclaring the constante- which is not necessary for the types Arrayand Object, of which I modify the value directly, because the assignment of a new value, with the use of the = (equality) is applied directly in the value element.

Browser other questions tagged

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