A variable is a named place in memory where you can store data to access it later. It's like a box with a gift inside: this is also a kind of variable!
Every variable has a name to distinguish it from other variables. It is possible to access a value by its variable name.
Variables are one of the most often used elements of programs, so it is important to understand and use them well.
letdefines a mutable variable the value of which can be changed as many times as needed;
constdeclares a constant whose value you want to forbid overwriting.
When you declare a variable, you must give it a name after one of these keywords. It is good practice to assign a variable name that describes its contents. Always try to choose meaningful and readable names for variables to make your code easy to understand.
Let's try to create a mutable variable and understand why it is called that. Let's call it
month and put data into it using the
let month = "November"; // define the variable and assign it a value
In variables, it is possible to store any type of data. In this case, we saved the string
"November". You can refer to this line if you use a variable name:
let month = "November"; console.log(month); // outputs the content of the variable to the console
This code outputs the contents of the variable into the console. In our case, the result will be the string
"November" - you can check it yourself.
The case of a name is important: `month` is not the same as `Month`.
Now let's try to change the variable and output a new value to the console:
let month = "November"; month = "December"; console.log(month); // December
As you can see, the value of the variable has changed. Now it stores the string
In older scripts, you may see that the
var keyword is used instead of
let. It looks like this:
var age = 23;
This is an outdated way of declaring a variable. The
var keyword is not bad per se, but there is no need to use
var in new projects. You can also find code when variables are declared without keywords:
age = 23;
It's not a good practice either. In the future, we will consider why it is a bad idea to declare variables without keywords or with
var, but for now, it is more useful to get acquainted with the constants.
Declare the immutable variable called
language and add the string
If we wanted to change the value of a variable created with the
const keyword, we would have failed and got an error:
Now that you know about variables, you can create more complex and exciting programs.
You can support me on Buy Me a Coffee