DEV Community

Arina Hovhannisyan
Arina Hovhannisyan

Posted on

About variables in JavaScript, differences between var, let, const. What are scopes?

Variables mean that they can vary, and they are used to name the stored memory locations. In JavaScript, they can be created by three different keywords:
VAR
LET
CONST
They can store different types of data, like one’s name, email, a number, a string, a true or false boolean, an object.
However, initially only VAR was created, but due to many issues connected with it, developers created LET and CONST. And the question that comes afterwards is about the difference between these three variables. But, firstly, we should clarify the meaning of the scopes.
Scopes can determine the accessibility of the variables in distinct parts of the program. In JavaScript are two types of scopes: local and global.
Global scopes let the variables be anywhere in the program. This type of scoped variables are those declared outside of the block or function. In contrast, if the variable is declared inside of a function or a block, then it is locally scoped. This means that variables declared inside of the block or the function are available only in that part. But, in their turn, locally scoped variables are divided into two types: function scoped and block scoped. As their names tell, the function scoped variables can be used only inside of the function. And the block scoped ones are accessible only inside of it, even if the block can be in the function.
Now let's talk about those three variable keywords. There are some differences between them, one of which is about their, let’s say, location. ‘’Var’’ is both a global and function-scoped variable, meanwhile ‘’let’’ and ‘’const’’ are block-scoped ones. Another difference is about their reassignment. Both ‘’var’’ and ‘’let’’ can be reassigned during program execution, while ‘’const’’ can’t be reassigned and it is immutable.

Top comments (0)