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.