Most apps keep track of pieces of information
that change and update as you use the app. For example,
a simple game can keep track of your score and the number of lives you have left
as you play.
Apps utilize a computer's memory to do this and learning
how to use that memory when programing is a really powerful skill.
The most common way to leverage a computers memory is by creating a variable.
Now a variable is just a container
for storing a value, and once created,
the variable is stored in the computer's memory and it can be used.
An update repeatedly throughout your program.
As an example, let's use variables to keep track of the score
and the number of lives remaining in a hypothetical game.
Creating the variables really easy.
You use the word VAR followed by a label that you want to give the variable.
You can choose any label you want, but it helps to choose
a word that describes the variable that you want to store.
So let's label our first variable score to keep track of the score
and then we can create a second variable to keep track of lives.
Remember these labels,
you'll need them later in order to use the variables
We can assign a value to the variable using the equal sign.
Like this.
Let's set our starting score to zero and our number of lives to three.
Remember, you only need to use the VAR at the time
that you want to create a new variable.
A common mistake is to use it every single time
you want to refer to a variable. Also, be sure to avoid mixing up the order.
When you call your variables, the labels got to
appear on the left and the value have to appear on the right.
Like this.
Not like this.
Often you'll want to create a variable and assign it a value right away.
There's.
There's a really convenient way to do this with one line of code.
This is sometimes called initializing the variable.
It's important
to realize that this single line of code is actually doing two things.
First, creating the variable and then second, assigning it a value.
Let's take a moment to talk about the equal sign.
In many coding language, the equal symbol is called the assignment
operator, because you use it to assign a value to a variable.
And you're probably used to seeing it in math, but in programing it's a little bit
different If you were in math class and you saw this,
something would definitely be wrong.
Score can't possibly equal both five and seven,
but this is not math class with code.
A computer is executing a set of instructions one after the other.
So this would just tell the computer to set a score to five
And then on the next line, set it to seven.
In an effort to avoid confusion, programmers
often don't use the word equals when we see this sign.
Instead, they use the phrase gets the value.
So when we see a statement like this, we could read it as score
gets the value seven or score get seven for short.
Some programing languages
avoid the confusion entirely by representing the assignment statements.
With a sideways arrow like this
To use
a variable, you just refer to it by its label.
Anywhere the variable name appears in the program, the value
currently stored in the variable will be retrieved from memory.
The tricky part is that when using a variable,
you often can't see visually what its value is. To bypass that problem.
You can use write
or console dot log to display the value like this
This is a really handy trick.
But remember, there's a difference between showing the value
of a variable and writing out the variables label.
When text appears in quotes, you're telling the computer
to write those literal ASCII characters on the screen like this
But on the other hand, without quotes,
the computer assumes that you're referring to a variable called score
and it will attempt to retrieve its value if you haven't made that variable.
Then the computer will give an error.