If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

Storing data in variables

Computer programs instruct computers how to process data.
Sometimes the data comes from a user, like when a game asks you to pick a difficulty level and music volume:
A diagram of a laptop with a game settings interface. The label "volume" is displayed above a slider that is 30% filled. The label "difficulty" is displayed above three buttons, "easy", "medium", "hard", with the "easy" button selected. An arrow points from the laptop to a table with 2 rows. The first row has "volume: 3" and the second row has "difficulty: easy".
Other times, the data is already stored in the program, like when that same game starts off with a score of 0 and 3 lives:
A diagram that starts with a table of 2 rows. The first row has "score: 3" and the second row has "lives: 3". An arrow points from the table to a laptop with a game playing screen. The screen displays a score of 0 in the upper left and 3 hearts in the upper right.
Either way, the program can store that data in variables. Each variable has a name, a value, and a type. The value might change over time, and that’s why its “variable.”
That game is using at least four variables:
Many variables store numbers and strings, like the ones above. Variables can also store other types of data, like lists, dictionaries, and Boolean values (true/false).
We'll start by learning numbers and strings, and dive into advanced types later. First things first, let's write some code!

Assigning variables

Here’s how we create a variable named score in JavaScript:
var score = 0;
That line of code is called a statement. All programs are made up of statements, and each statement is an instruction to the computer about something we need it to do.
Let's add the lives variable:
var score = 0;
var lives = 3;
📝 See equivalent code in: App Lab | Snap | Python
Now our code is using two statements to store two variables with two different values. Behind the scenes, the computer is storing each value in a different place in memory and the variables point at that memory location.

Displaying variables

How do we make sure the computer is actually storing those values in memory? We can ask it to display the values.
As we learned before, one way to display on Khan Academy is with the println() procedure:
📝 See equivalent code in: App Lab | Snap | Python
When we instruct the computer to println(score), the computer has to look in its memory for the current value of that variable, and then once it finds it, it can display it in the console.
🔍 Try it yourself: Edit the code to ask the computer to find a variable that it doesn't know about yet. What happens?

Re-assigning variables

In a game, the player's score and their number of lives don't stay the same. The score typically goes up, and the lives typically go down. That means we need to be able to change the value of those variables later.
We can re-assign a variable to a new value using code similar to our initial assignment:
score = 5;
Now the score variable is storing the value 5 instead of the initial value of 0.
Notice that when we reassign in JavaScript, we no longer put the var in front. That's not the case in all languages, so it may be slightly different in another language you're learning.
Here's a program that creates the two variables, re-assigns one of them, and displays values along the way:
📝 See equivalent code in: App Lab | Snap | Python
This program is interesting because it contains 2 lines that look exactly the same: println(score). Yet all of the outputted lines are different; how can that be?
That's because the computer runs each statement in order, and the value of the score variable changes over time.
Let's step through this program, one statement at a time:
1var score = 0;Initializes the variable score to 0
2var lives = 3;Initializes the variable lives to 3
3println(score);Displays current value of score: 0
4println(lives);Displays current value of lives: 3
5score = 5;Re-assigns the variable score to the value 5
6println(score);Displays current value of score: 5
When we're first developing a program, we often display the value of variables to double-check the state of the program. Are the variables storing what we think they're storing, or did our code do something unexpected along the way?
🔍 Try it yourself: edit that code above and see how the output changes.

Pseudocode for variables

This pseudocode represents assigning a variable:
a ← expression
Whenever you see that pseudocode, it means that the variable a (or whatever it's called) is assigned the value of expression.
For example, you might see pseudocode like this:
age ← 21
That means the variable age is assigned the value 21.
Let's look at the equivalent code in a few textual languages:
JavaScriptvar age = 21;
Pythonage = 21
Variable assignment is also a concept in block-based languages:
Screenshot of variable block from Snap, says "set age to 21"
Screenshot of variable block from AppInventor, says "initialize global age to 22"
As you can see, the code to assign variables is very similar across languages. There are some differences though, and that's why we use pseudocode: to communicate the concept and not worry about the exact syntax.
Check your understanding
What's the pseudocode equivalent of this JavaScript?
var x = 200;
Choose 1 answer:

🙋🏽🙋🏻‍♀️🙋🏿‍♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!

Want to join the conversation?