JavaScript Variables
Knowing how to work with JavaScript variables is a must for every web and JavaScript developer. In this tutorial, you will learn the basics, how to declare, initialize and re-assign JavaScript variables and how to use var
, let
and const
. You will also learn about scope.
Introduction to JavaScript variables
Similarly to many other programming languages, JavaScript also has variables. An easy way to think about variables is to think about them as storage containers. Each of these containers has a name, a variable name. You can use these containers to store various types of data, such as numbers, strings, objects, arrays, etc.
The best thing is that you can use the data they are storing later. When you want to use these data later you do it by using the name of some specific container, or variable. This is also called referencing a variable. When you reference a variable, use its name, JavaScript will return the value is assigned to that variable, data stored in that container.
Variable declaration, initialization and re-assignment
One thing before we take a look at the types of JavaScript variables you can use. We should briefly talk about declaration and initialization. When it comes to JavaScript variables, or variables in general, you will encounter these two terms very often. What these terms, declaration and initialization, actually mean?
Variable declaration
Variable declaration means you are creating a variable with or without assigning it any value. You can declare variable with var
, let
or const
keywords. We will take a look at each of these later. Now, let’s focus on declaration itself. In JavaScript, it is allowed to declare, or create, multiple variables at the same time.
When you want to do this there are two things to remember. First, you must use the same keyword, var
, let
or const
, for all variables. You use the keyword only once, at the beginning of the line. Second, you separate variables with commas. Otherwise, you can declare, or create, all variables individually. This allows you to use different keywords, if you want.
Variable initialization
Variable initialization means you are storing some value in a variable. You can do variable initialization at the time of variable declaration, when you create the variable. Or, you can initialize it later, when you assign the variable a value. There is one exception. You can initialize later only var
and let
.
You can’t initialize const
variables later. You can initialize const
, assign it a value, only at the time you declare it. Otherwise, JavaScript will throw a syntax error. We will talk about this later in this tutorial, in the section dedicated to const
variables.
Re-assigning JavaScript variables
In case of var
and let
, you can also re-assign them. You can assign them different values after you initialized them, i.e. assign them some value for the first time. You can’t re-assign const
. Re-assigning JavaScript variables uses the same syntax as initializing them, i.e. assigning them value for the first time.
You specify the variable you want to re-assign. Variable whose value you want to change. Then, you assign it a new value, using equal sign and the value. That’s it. When you try to access that variable, you will get that new value.
Accessing JavaScript variables
That was about creating and updating JavaScript variables. Another thing you need to know is how to access them. Or, how can you access the value that is stored in a specific variable. You can do this by using the name of the variable that contains the value you want to access. This is also called “referencing a variable”.
JavaScript variables and scope
In JavaScript, there are two types of scope. The first type of scope is global
. The second one is local
. Global scope is a scope, or environment, outside any function. Any variable declared in global scope is visible and accessible for the rest of your code. Local scope is a scope that exists within a function.
Local and global scope
Every time you create a function JavaScript also creates a new local scope for that function. This scope, also called Function scope
, is then applied to all variables you declare inside that function. Any variable declared in local scope is called local variable. Local variables are not visible or accessible outside that scope.
When you try to access some local variable from the global scope, the outside, JavaScript will throw reference error about variable not defined. So, remember that when you declare a local variable, you can use that variable only in that scope, or in that function. Otherwise, your code will not work.
JavaScript variables declared in local scope are not visible, and accessible, outside that scope. However, they will be visible, and accessible, if you create new local scope inside the previous local scope. If you create a function inside a function that second, child, function can work with variables declared inside the outer, parent, function.
“Nested” local scopes
One thing to remember in the terms of these “nested” scopes. Variables you declare inside outer functions are visible, and accessible, in inner functions. Variables you declare inside inner functions are not visible, or accessible, in outer functions. It is like local and global scope.
Anything created in a global scope, or outer functions, is visible, and accessible in local scope, or inner functions. Anything created in a local scope, or inner functions, is not visible, or accessible, in global scope, or outer functions.
So, let’s say you have one inner function A inside outer function B, and function A contains some variables. These variables, declared in inner function A, will not be accessible in outer function B.
Block scope, var, let and const
So far, we focused solely on global
and local
scope. From this angle, the var
, let
and const
variables work very much the same. Well, not so fast. Aside to global
and local
scope, there is also a block
scope. This type of scope is limited to block statements and expressions.
The var
, for example, doesn’t care about block scope. When you declare a variable using var
in a block scope, like in an if
statement, it is still visible everywhere. What about let
and const
? These are different. They respect scope. Variables declared with let
or const
will be visible only inside that block scope.
When you try to access any let
or const
variable outside the block scope JavaScript will throw a reference error about variable not being defined. When you try to access var
variable outside the block scope you will get the value.
This is one reason JavaScript developers prefer let
and const
over var
. With let
and const
, you have more control over code. The rules of visibility, and accessibility, for JavaScript variables are much more strict. It is less likely to happen that one variable will collide, or overwrite, another. Especially in case of const
.
Var
There are three types of JavaScript variables you can use. These are var
, let
and const
. The first type of variable is var
. This type of variables has been in JavaScript since the beginning. It is also usually the first of JavaScript variables people get familiar with. Well, at least it used to be this way.
Nowadays, thanks to popularity of let
and const
, var
is slowly losing traction. Nonetheless, since var
is still one of JavaScript variables, it is still good to know about how it, and how it works. Knowing about how var
variables work is also necessary if you want to really understand why let
and const
are usually better choices for JavaScript variables than var
.
With var
, you can either declare a variable first and initialize it later or you can declare it and initialize it and the same time. The way to declare variable using var
is very simple, just like using the rest of JavaScript variables. You use the var
keyword followed by the variable name.
If you want to only declare a variable without initializing it, here is where you stop. Otherwise, you also initialize it, i.e. assign that variable a value. When you declare a variable using var
, you can re-assign that variable any time later. There is no restriction that could prevent you from changing your variables declared with var
.
When you declare new variable using var
make sure to not to use the same name for another variable, declared in the same scope. Otherwise, it could happen that you declare new variable that uses the same name as another variable you declared earlier. If both variables are either in a global, or in the same local, scope the newer will overwrite the older.
So, make sure to either use different names or different scopes. Also remember that var
don’t work with block scope. So, creating two variables with the same name, one in a global scope and one in a block scope, will lead to collision. The newer variable will again overwrite the older.
Let
The second type of variable is let
. This type of variable is new. It was added to JavaScript with ES6 specification. The let
variable works in a similar way to var
. Similarly to var
, with let
you can also either declare a variable first and initialize it later or you can declare it and initialize it and the same time.
Also similarly to var
, when you declare variable with let
you can change it, re-assign it, any time you want. There is, again, no restriction that could prevent you from changing your variables declared with let
.
About the differences. As we discussed in the section about JavaScript variables and scope, let
variables are restricted to block scope. let
variables are not accessible outside the block they were declared in. So, when you use variables with the same name, one in a block scope and the other outside it, you don’t have to worry about one overwriting the other.
Const
Similarly to let
, const
is also new addition to JavaScript, added in ES6 specification. Another thing const
variables share with let
is that they are also restricted to block scope. So, when you declare a variable using const
inside a block, it will be visible, and also accessible, only inside that block.
However, this is where the similarity with let
ends. The first difference is that, with const
, you can only declare a variable and initialize it the same time. You can’t declare a variable and initialize it later. JavaScript variables declared with const
are constants. They can’t be changed, or re-assigned, later.
For this reason, it is also not possible to declare const
variables and initialize them, assign them a value, later. If that, JavaScript will throw a syntax error about missing initializer in const declaration. If you try to change const
variable, JavaScript will throw type error about assignment to constant variable.
So, remember, when you use const
variables, you have to initialize them right when you declare them. And, you can’t re-assign these variable later.
Partially immutable
I mentioned that it is not possible to re-assign const
variables. That’s true. Some developers also think that const
variables, or their values, are immutable. This is not true. While you can’t re-assign const
you can change its value. Well, partially. You can’t change the value of const
if the value is a primitive data type.
For example, if the value of const
is a number
or string
, you can’t change it. You can’t change that number, or string, into a different number, or string. However, if the value of const
is an object you can change properties of that object. Or, if it is a collection such as array you can change the content of that array
.
That said, that doesn’t mean you can change the object, or array, itself by re-assigning it. You can’t. You can change only the content of that object or array stored inside a const
.
Naming variables
You know how to declare, initialize and re-assign JavaScript variables. You also know what types of variables can you use. The last thing you need to know is how to create a valid variable name. Fortunately, variable names are very flexible and here are only three rules you have to follow.
First, a variable name can start with a letter (lowercase and uppercase), underscore _
, or dollar sign $
. Second, variable name can’t start with a number. However, it is allowed to use numbers in variable names after the first letter, as well as letters (lowercase and uppercase), underscores, or dollar signs.
Third, don’t use any of JavaScript’s reserved keywords. If you break any of those rules, JavaScript will throw a syntax error and your code will fail to run.
Conclusion: Introduction to JavaScript Variables
Congratulations! You’ve just finished this tutorial on JavaScript variables. By now, you know what are variables and how to declare, initialize, re-assignment and access them. You also know how to use var
, let
and const
and how each of these types works with scope.
Last updated
Was this helpful?