What is the difference in the declaration of variables using const?

Asked

Viewed 408 times

-4

In the some features have been added, among them is the const the visa is used in the same way as the visa var, to declare variables.

What is the difference between declared variables using this const or not?

  • Hell, I’d searched three times and I couldn’t find anything, this search for Stackoverflow is half bad ^^

  • But there’s actually a question between letand var. Does not cover the const. Would that be duplicated yet? And the Const?

  • I can’t find any question on the site that refers to what the const. @Maniero ♦

1 answer

0

Well, fundamentally speaking, the const serves not only to define that the variable has the global scope, but mainly to make the value of the variable immutable, so that it cannot be changed and because of this, have a constant value.

Explanations

Modifier const

As some already know of other languages, the const refers to the word constante which means immutable or always equal. The scope of const in question is a global scope, just as if there were no let nor var. So the reason this modifier exists is not for scope purposes but rather due to the fact that a variable declared as const becomes immutable, resulting in an error any attempt to assign it. However, there are two exceptions, if the value of the variable const is an object, you can still change the value of the attributes of such an object, but the object remains immutable, you cannot assign another object to it. Also for Array’s, you can add more elements to the Array assigned to it, but you cannot assign another Array to it.

Example

VARIAVEL_GLOBAL_COMUM = 0;
const MINHA_CONSTANTE = 1;

MINHA_CONSTANTE = 2; //Isso resulta em um erro. Você não pode alterar o valor de uma constante.
VARIAVEL_GLOBAL_COMUM = 3; //OK.

var MINHA_CONSTANTE = 4; //Resulta em erro porque você não pode utilizar o mesmo nome reservado para uma constante definida.
let MINHA_CONSTANTE = 4; //Resulta em erro porque você não pode utilizar o mesmo nome reservado para uma constante definida.

const OBJETO_CONSTANTE = { atributo: 'texto' };

OBJETO_CONSTANTE = { outroAtributo: 'texto' }; //Resulta em erro. Você não pode atribuir outro objeto para a constante já definida.
OBJETO_CONSTANTE.atributo = 'Outro Texto'; //Funciona. Porque você pode alterar o valor do atribuito dentro do objeto já atribuido.

const ARRAY_CONSTANTE = [1,2];

ARRAY_CONSTANTE = [1,2,3]; //Erro. Você não pode atribuir outro Array para o array constante já definido.
ARRAY_CONSTANTE.push(3); //Funciona. Você pode adicionar um valor no Array já atribuido na constante.

Note that the modifier const is not used for different scope purposes but rather for variable freezing, but given that it is possible to receive an Object that may have the value of its changed attributes and an Array that may have more elements added.

Completion

The const has a global scope, as well as declaring a variable without modifier, but its main function is to make variables immutable.

Sources/References:

Browser other questions tagged

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