JavaScript variables

The basic concept of any programming language is variable. A variable is a container in which we can store any data, for example, strings or numbers.

Each variable must have a name, the latter can consist of Latin letters, numbers, symbols $ and underscores. The first character of the variable name must not be a digit. Variable names examples: str, my_str, myStr, a1, $, $a, $$a.

Using of variables

In order to use a variable, you should first declare it: put the keyword let before its name. Let's declare, for example, the variable named a:

let a;

After the variable declaration, you can write to it (they say to assign to it) some value, such as some number or string.

Writing data to a variable is done using the assignment operation =. Let's, for example, write the number 3 into the variable a:

let a = 3;

And now let's display the contents of this variable on the screen using the alert function:

let a = 3; // declaring a variable and assigning a value to it alert(a); // shows 3

It is not necessary to write the value into the variable immediately after the declaration. You can first declare a variable and then assign a value to it:

let a; // declaring a variable a = 3; // assigning value to it alert(a); // displays the value of the variable on the screen

As you see, let is written only once before a variable name - when you declare this variable. To use a variable further, you need only write the name of this variable.

Create the variable num and assign the value 123 to it. Display the value of this variable to the screen using the alert function.

Declaring of multiple variables

Let's declare several variables:

let a = 1; let b = 2; let c = 3;

We can simplify the code above if we write let one time and listing then the required variables with their values after it, in such a way:

let a = 1, b = 2, c = 3;

You can first declare all variables, and then assign values to them:

let a, b, c; // declare all 3 variables // Assigning values to variables: a = 1; b = 2; c = 3;

Try all the described ways of declaring variables by yourself.

Variable value changes

Suppose we have a variable to which we have assigned some value. We can apply again the assignment operation then and assign an another value to this variable:

let a; // declare a variable a = 1; // assign the value 1 to it alert(a); // shows 1 a = 2; // now assign the value 2, overwriting the value 1 alert(a); // shows 2

That is, the value of a variable is not something rigidly attached to it. We can freely write some data to a variable, read it, then write something else - and so on.

Create the variable a. Assign the value 10 to it, display it on the screen. Then write the value 20 into it, display it on the screen.

Multiple let for single variable

The same variable cannot be declared multiple times with let. For example, the following code will result in an error:

let a = 1; alert(a); let a = 2; alert(a);

Here are two solutions to the problem. You can just enter two different variables:

let a = 1; alert(a); let b = 2; alert(b);

But you can also declare the variable a first and then operate on it:

let a; a = 1; alert(a); a = 2; alert(a);