The next question: When should you use var, let and const in ECMA Script 6? It partially answers my question, but not completely.
Reading the accepted answer I have understood that:
let
declare a local scope variableconst
declare a constant inside a block (inside a function?)var
I did not understand if it is local or globalAnd if it is not set
nada
, what is the range? In another related question I read that when nothing is put, a global variable is being declared.
I'm a bit confused and would like to understand the exact scope of each form of variable declaration, in a simple answer if possible.
On the other hand, I don't know if this would be a topic for another question, but I don't quite understand what the concept of global would be in Javascript . Current DOM tab? All open tabs? All open browsers and tabs?
Summary for those who do not want to read the whole explanation:
const
defines constants, but if we are not careful they can be modified. It has block scope, likelet
.var
defines variables with a function scope (the current context).let
defines variables with a block scope and from the line on which they are declared.const identificador = ...
Create a constant . As in other languages such as Java or C#, if the constant is an object , its attributes are modifiable , so we must be careful with what we do:
var identificador ...
Javascript is a bit special for certain things and this is a good example of its oddities: the variable can be declared when using it, at the beginning of our code or at the end; it won't really matter because the interpreter will "hoist" the declaration to the start of the current scope/context (this is called hoisting and it also affects function declarations). The scope is always function, not block :
Also, it doesn't matter if we declare it multiple times, the extra declarations will be ignored:
The behavior of the values of a variable used with closures and asynchronous callbacks often confuses inexperienced programmers, I give an example and how to solve it:
let identificador = ...
And we come to the latest Javascript novelty, which behaves in a similar way to what a variable declaration is in other languages such as Java or C: the variable can only be used from its declaration and its scope is local to the block , not to the function :
Unlike
var
, no duplicates are allowed:And what happens if we use an identifier without declaring it? Well, we have two scenarios: Javascript "normal" and Javascript "strict"
This example demonstrates both scenarios, and also helps answer the question "What is global in Javascript?" If we're running in a browser, the global object is always
window
. If we're running the code in NodeJS, the global object isglobal
.But beware,
window
it is not the entire browser window, but the current tab. This is due to the fact that until the arrival of Firefox there were no tabs and each page that we opened in a browser forced us to have a new window open. For security we cannot share variables between different tabs.var will declare a local variable, i.e. if you declare
x will only have scope inside myVar function, if you declare at the beginning of all js
var x = 0;
outside of any function it will have scope in all js.If you don't put var you declare a global variable, now what is the difference between declaring a global variable and a local variable but in all the js? That if you put it
var x = 0;
at the beginning of the js it will only have scope in the current js, if you declare it global it will have scope in all the js that are declared in the html. That is, if I declare in a js Ix = 0;
can use the variable in another jsx
and it will have the value of 0Const could be as a global variable just like
x = 0
, but the big difference is that since it is a constant it cannot be redeclared, that is, if you putconst x = 0;
it, you cannot later put x = 2 in a function.As this page explains about it in const, it can be changed in cases of array, that is, fill it or remove elements https://mathiasbynens.be/notes/es6-const
Let is used to declare a bit more local variables, as this answer explains https://stackoverflow.com/questions/762011/whats-the-difference-between-using-let-and-var-to-declare-a-variable
practically var takes the parent block so to speak, if you use a let in a for inside a function
let will only be available inside the for, if you do it with a var, it will be in the whole function
In the early days of JavaScript, var was the only way a variable could be created. Much later let and const were born with the EcmaScript 6 standard.
var-characteristics
It can be reset.
It can be reassigned.
Its reach is global.
This would also allow us to create var variables with the same name within multiple if or other nested structures. Even by its scope, if we execute the following code, we will not get any errors.
This is not at all intuitive, which could cause us problems when programming. So the advice is not to use var .
Characteristics of let
Cannot reset.
It can be reassigned.
Its range is block.
Characteristics of construction
Cannot reset.
It cannot be reassigned. Being a constant, this is its main characteristic. We cannot change the entire element of our constant.
It is not immutable. We can change the properties of the constant element, but not the whole element. We will explain this by means of an object.
For the use of operations on functions.
You should use the consts inside the functions if they are values that are only used as a reference, if this value is not going to change in the functions and the let you use them when you need to change a value. Both const and let are widely used in functions, either for code readability or the use of a counter or accumulator variable.
for exports
Normally when importing a namespace (file, library, etc), it is required to expose certain functions, variables or constants and these will not change in the application, therefore they would be const .
note: do not forget to put in capital letters for const reference, since they are values that are used for any application.
Things to keep in mind
This answer is a more practical example of using let and const in javascript.
Resumen para los que no quieran leer toda la explicación:
const define constantes, pero si no tenemos cuidado se pueden modificar. Tiene alcance de bloque, como let.
var define variables con un alcance de función (el contexto actual).
let define variables con un alcance de bloque y a partir de la línea en la que se declaran.
Si no declaramos un identificador, se creará como una variable global a menos que nuestro código sea declarado "estricto".