Variables and Data
Learn how computers store and use information using variables -- the same idea as letters in algebra!
What is a Variable?
A variable is like a labelled box that stores information. You give the box a name, and you can put something inside it. You can also change what's inside later!
Variables are Like Labelled Boxes
In JavaScript, we create a variable using the word let:
let score = 42; // A number
let name = "Alex"; // A text string
let isRaining = true; // A boolean (true/false)
Maths Connection: Variables in Algebra
In Maths, you already use variables! When you see:
x + 5 = 12
Here, x is a variable. It stands for the number 7.
In coding, it's exactly the same idea! let x = 7; means "store the number 7 in a box called x." The only difference is that in code, variables can store text and true/false values too, not just numbers.
Types of Data
Variables can store different types of information:
Number
Numbers for counting and calculating. Can be whole numbers or decimals.
let age = 10;
String (Text)
Text wrapped in quotation marks. Letters, words, or sentences.
let name = "Alex";
Boolean
Only two values: true or false. Like a light switch -- on or off.
let sunny = true;
Try It: Changing a Variable
Variables can change. Watch what happens when we update a score:
let score = 0; // Start with 0 points
display("Score: " + score); // Shows: Score: 0
score = score + 10; // Add 10 points
display("Score: " + score); // Shows: Score: 10
score = score + 5; // Add 5 more points
display("Score: " + score); // Shows: Score: 15
score = score - 3; // Lose 3 points
display("Score: " + score); // Shows: Score: 12
Try It: Your Name in a Variable
Type your name below to see how string variables work:
let name = "[your name]";
let greeting = "Hello, " + name + "!";
display(greeting);
display("Your name has " + name.length + " letters.");
Key Vocabulary
Variable
A named container that stores a piece of data. The value can be read and changed.
Data Type
The kind of value stored in a variable: number, string, or boolean.
String
Text data wrapped in quotation marks. E.g., "Hello"
Boolean
A value that is either true or false. Named after mathematician George Boole.
Assignment
Using = to put a value into a variable. E.g., score = 10
Input / Output
Input = data going IN to the program. Output = results coming OUT.
Worked Examples
What is the final value of total?
let total = 20;
total = total + 15;
total = total - 8;
Step 1: total starts as 20
Step 2: total = 20 + 15 = 35
Step 3: total = 35 - 8 = 27
Answer: The final value of total is 27
What data type is each variable?
let temperature = 23.5;
let city = "Sydney";
let isSunny = false;
temperature: Number (23.5 is a decimal number)
city: String ("Sydney" is text in quotes)
isSunny: Boolean (false is a true/false value)
What does this code display?
let firstName = "Jo";
let lastName = "Smith";
let fullName = firstName + " " + lastName;
display(fullName);
Step 1: firstName = "Jo"
Step 2: lastName = "Smith"
Step 3: fullName = "Jo" + " " + "Smith" = "Jo Smith"
Answer: It displays "Jo Smith". Joining strings together is called concatenation.
Knowledge Check
Select the correct answer for each question.
Question 1
What is the value of x after this code runs?
let x = 5;
x = x + 3;
x = x * 2;
Question 2
What data type is the value "42" (with quotes)?
Question 3
What does this code display?
let a = 10;
let b = 3;
display(a + b);
display(a - b);
Question 4
Which of these is a valid boolean value?
Question 5
What does this code display?
let colour = "blue";
colour = "red";
display(colour);
Key Concepts Summary
- ●A variable is a named container that stores data. Create one with
let. - ●Three main data types: Numbers (42), Strings ("text"), and Booleans (true/false).
- ●Variables in code are just like variables in algebra -- they represent values that can change.
- ●You can change a variable's value using
=(assignment). - ●Strings can be joined together using
+(concatenation).