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:
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 Array
or 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.
The flag
of 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 Array
and 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.
Related or duplicate: How to declare a constant in javascript?
– Jéf Bueno
Related or duplicate: How to make a constant object in Javascript
– Jéf Bueno
I didn’t understand the closing, or the closing acceptance since the linked question doesn’t say anything about array.
– Maniero
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.
– Valdeir Psr