Variables and constants
We have talked a lot about types and how we can use them logically. Now we will talk about storing this data, so that we can use them later on, and using them again and again.
In JavaScript previous to EcmaScript 6 (also called JavaScript 2015), we had the variable var
. var
is a JavaScript keyword, so that when you write var
, JavaScript knows that this is an variable. There are a lot of other keywords also, which are reserved, meaning that you can't name a variable or function with that keyword.
A variable can store a string, number or boolean. Later we'll explore arrays and objects, which can also be stored in a variable. Store is not quite right though. A variable only references a value.
When declaring a new variable, you start with the keyword var
followed by the variable's name and it's value.
It is possible to declare a variable without using the var
keyword if the variable name haven't been declared previously. This is not good practice. To avoid such errors, write "use strict";
at the top of the script.
The equal symbol says that the variable name on the left side should be equal to the value on the right. Variable names can be any word that isn't a reserved keyword and they can't include spaces. Digits can be part of the variable name, but they can't be the first character. Variable names also can't have a punctuation marks in it, except $
and _
.
A variable that references a value is not tied to that value forever. You can reassign a value by typing the variable's name, followed by a new value:
With EcmaScript 6, we were introduced to the variable let
and the constant const
. let
works similar to var
in that it can be reassigned, but let
is scoped differently that var
. What does this mean?
Scoping is the code which a variable lives inside. Don't worry if that wasn't clear, I have an example! But first the difference between those two in words. var
is scoped to the nearest function block, while let
is scoped to the nearest enclosing block. Both variable types are global (visible from wherever in the code) if they are declared outside any block.
Okay, so the example:
If you're writing in "strict mode, i.e. that you've written "use strict";
at the top of the document, var
will let you re-declare the same variable within the scope. let
will not let you do this.
And then we have const
, which is a constant. That means that the value can't be changed. How is this useful, you may ask? Let me explain. First, you declare a constant this way:
If you try to reassign the constant's value, you will get an error.
To avoid errors and to get better performance, you should use `const` whenever possible. But sometimes, e.g. in a for loop, you need to use let
or var
. You can set the constant's value to an array, and still change the content of the array, but not reassign the constant to another value:
You might have noticed that my one-word variables are in all lower case, and that the ones with more than one word have the first word written with lower case and that the following words start with an upper case letter. This is called camel casing, and is the naming convention in JavaScript, e.g. "myAge".
There are no rules for naming, other than the ones mentioned earlier, but it makes naming easier if everyone follows the same convention. Remember to give your variables and constants a meaningful name, so that it's easy to understand what their value mean.
To summarise, we have two variables and one constant: var
, let
and const
. var
is function scoped, let
and const
are block scoped. const
can't be reassigned, var
and let
can.
Safari versions below version 9 does not support JavaScript ES6 fully, thus let
and const
would not work. You can see the compatibility table for ES6 here.
Last updated
Was this helpful?