Why This Matters
Every program needs to remember things. When you calculate a total, track a score, or store a user's name — you're using variables. They are the most fundamental building block of all programming. Without variables, a program can only produce the same fixed output every time.
Understanding variables means understanding how a computer holds onto information while your program runs.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
The lifecycle of a variable: declare, assign, store, reassign, use in expressions.
Code Example
// Declaring variables
let score = 0; // A number that can change
const playerName = "Alice"; // A string that stays fixed
let isActive = true; // A boolean (true or false)
// Reassigning a variable
score = score + 10;
isActive = false;
// Using variables
console.log(playerName + " has " + score + " points");
console.log("Active: " + isActive);Interactive Experiment
Try modifying the code above:
- Change the initial value of
score— what happens to the output? - In JavaScript, try reassigning
playerName(declared withconst) — what error do you get? - Create a new variable called
leveland print it alongside the score.
Quick Quiz
Coding Challenge
Write a function called `fahrenheitToCelsius` that takes a temperature in Fahrenheit and returns the equivalent in Celsius. Formula: C = (F - 32) * 5/9
Real-World Usage
Variables are everywhere in production software:
- E-commerce:
cartTotal,itemCount,shippingAddress,discountCode - Games:
playerHealth,currentLevel,highScore,inventory - Web apps:
isLoggedIn,userName,sessionToken,theme - APIs:
requestBody,responseStatus,rateLimitRemaining
Every program you will ever write or read uses variables. Mastering them is step one.