programming foundations20 min

Variables

Understanding how programs store and manage data

0/9Not Started

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

Declarelet x
Assignx = 10
Memorystores 10
Reassignx = 20
Usex + 5

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

Code
// 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 with const) — what error do you get?
  • Create a new variable called level and print it alongside the score.

Quick Quiz

Coding Challenge

Temperature Converter

Write a function called `fahrenheitToCelsius` that takes a temperature in Fahrenheit and returns the equivalent in Celsius. Formula: C = (F - 32) * 5/9

Loading editor...

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.

Connections