Variables
Last updated
Was this helpful?
Last updated
Was this helpful?
You know what a variable is.
You know what we can put into a variable.
You can declare variables and change their values.
You can think of a variable as a container, such as a box. You can put stuff into a variable, just like you can put stuff into a box. In addition, the box has a label on it, which is the name of the variable. The fact that our box has a name is very useful: at any time, we can access the content of the box by calling its name. We'll see later how we can access the content of variables that we use in our programs.
In line 1, the variable's name is devices
. Finally, we tell our program that a statement is complete by adding a semicolon to the end of it. Great, we just declared our first variable! But ... what is a variable?
The short answer is: almost anything. A variable can contain simple things like text or numbers. But it can also contain more complex things, like lists, functions or objects. We'll learn more about that later in this course.
To answer this question, we step forward into line 22 of our program:
The symbol =
is how we assign a value to a variable. The above statement stores whatever the variable (or container) newDevices
contains on the variable devices
, which we declared at the beginning of our program. When you think of the two variables newDevices
and devices
as containers, like a bucket, the statement above effectively puts the bucket with the name newDevices
into the second bucket with the name devices
. Afterwards, we can still reference both buckets by their names, but we'll get the same content for both.
We define the variable devices
as a so-called global variable. A global variable is defined outside any function, and it is good practice to declare them at the top of your program, so we can easily find them.
The difference between a global and a local variable is their visibility. While we can access global variables from anywhere in our program, a local variable can only be used in the block where we defined it. This could be a function, for example. If you know we need a variable only within a function, it is good practice to define it locally within that function.
To stay in the container analogy, a global variable is a container that is known by everyone and everywhere. If you tell someone the container's name, everyone will know what you are talking about, and you can access the content of the container from everywhere. In contrast, a local variable is a container that is only known in a specific room. If you ask someone outside that room, she won't know what you are talking about. This also means that if you need the content of that container, you'll need to be in that room.
Read more about variables in JavaScript under the following link:
In JavaScript, we use the keyword var
to tell our program that we are about to declare a new variable. The keyword var
is followed by a name that we choose for our variable. The name is up to us, but we must follow .