A variable is a name created by the programmer to represent some form of data. When the interpreter reads a variable declaration, a section of memory is reserved for that variable. The information assigned to the variable, called the variable's value, will then be placed into that memory location. The variable can then be used to reference the data throughout the script, to either read the data, or modify it.
A value is the content of a variable. The variable's value is data represented by that variable. When computers temporarily store information in memory, it's stored in what is called a "memory address". The contents of the memory address is a binary number (collection of 1's and 0's) and is considered the memory address's value. Variables link to memory in the computer, and store their information in a memory address. This is likely (I assume) the reason the information stored in variables are called values.
There are different types of values, such as numbers, strings, arrays, and objects, which we will talk about in future sections.
You declare variables by using variable specific declaration keywords (such as var, let, or const) in front of your variable name. You get to decide what you want the name to be.
The keyword var is an older JavaScript keyword that is used to declare a variable. The more commonly used variable keywords now are let and const, which are newer keywords to JavaScript. You can basically use let the same way as var, but there are slight differences. Some of which involve an understanding of scope, which has to do with block statements, which we'll discuss in future sections.
You can assign a value to a variable by using the equal sign.
var someThing = 32;
Or, you can assign the variable and then later re-assign it:
var
someThing;
someThing
=
32;
someThing
=
64;
If you want to create and assign a variable, but don't want it to get reassigned later, that's what the variable keyword const is for.
In the example above, the variables are declared, but no values are assigned.
Variables can be declared without assigning values right away. Before a variable is assigned a value, it's default value type will be undefined. The word undefined is both a specific value type, and a keyword that represents that value type.
If a variable's value is undefined, that means that either a value wasn't assigned, and is missing, or the value is not what was expected.
Once you declare a variable, there's no need to declare it again, and you may use the variable, by itself, without the declaration keyword. For example, here, the variable is first declared, then it is assigned:
var
someVariableName;
someVariableName =
32;
Variables are useful for holding various types of information. A number, a sentence of text, an html element, or some other type of data. Variables make code writing quicker and easier.
For example, if you use the same sentence in different parts of your script, instead of writing the sentence over, and over, you can create a variable to represent the text...
let aUsefulSentence = "This is a really long sentence, I use it a lot, and I'd hate to have to write it out every single time I need to use it!";
... and the script will get that value from memory whenever it sees aUsefulSentence in the code.
let
p1Element
=
document.getElementById('p1');
let
p2Element
=
document.getElementById('p2');
let
aUsefulSentence
=
"This is a really long sentence, I use it a lot, and I'd
hate to have to write it out every single time I need to use it!";
p1Element.innerText
=
aUsefulSentence;
p2Element.innerText
=
aUsefulSentence;
The green text between quotation marks are called strings, which is a value type assignable to variables, we'll talk more about strings in a future section.
Variables can be used like an unknown in algebra. Let's do some algebra using JavaScript. Here we create two variables and assign them numbers. Then we create another variable and assign it the sum of the other two.
let
x
=
3;
let
y
=
2;
let
sum
=
x
+
y;
After the script is run, the value of sum will be 5.
Even though this looks like algebra, it's really not. They're really just variables. Variables x and y were declared. The above code is the same as the code below.
let
numberThree
=
3;
let
numberTwo
=
2;
let
threePlusTWo
=
numberThree
+
numberTwo;
One way of finding out what type of value a variable has is by using the keyword "typeof".
let
someVariable
=
3;
let
element
=
document.getElementById('elementId');
element.innerText
=
"The type of the variable is: "
+
typeof
someVariable;
In this case, the result will show:
The type of the variable is: number
In the next section, we'll talk about objects.