RadarTrek
Home/Courses/Think Like a Programmer/Variables: Labelled Boxes for Information
Lesson 02 / 9ยท6 minFree

Variables: Labelled Boxes for Information

How computers store and remember things

The first idea in every programming language: variables. A variable is just a labelled box. You put something in the box, give the box a name, and then you can refer to that thing by the name instead of the value.

๐Ÿ’ก

Variables are like labelled jars in a kitchen

You have jars on your kitchen shelf labelled "sugar", "salt", "flour". You don't need to know exactly how many grams of sugar is in the jar โ€” you just say "use the sugar jar." The name (sugar) points to whatever is currently inside. If you refill the sugar jar, the name still works. In programming, a variable is the label. The contents can change.

The same concept in every language

The syntax changes โ€” the idea doesn't. Here's storing someone's name across four languages:

Same variable, four languages

JavaScript:   let name = "Sarah"
Python:       name = "Sarah"
Java:         String name = "Sarah";
SQL:          DECLARE @name VARCHAR(50) = 'Sarah'
!

Notice what changes and what stays the same

In every case: (1) we choose a name for our box, (2) we put a value in it, (3) we can use the name later. The words "let", "String", "DECLARE" are just that language's way of saying "create a box." The concept is identical.

Types: what kind of thing is in the box

Not all boxes hold the same kind of thing. Programming languages distinguish between types of data:

  • Text (String) โ€” Letters, words, sentences. Always in quotes. "Hello", "Sarah", "42 High Street".
  • Number (Integer, Float) โ€” Numbers. 42, 3.14, -7. No quotes. Used for maths.
  • Yes/No (Boolean) โ€” Only two possible values: true or false. Is the user logged in? Is the form valid?
  • Lists (Array) โ€” Multiple things in one box, in order. ["red", "green", "blue"]. A shopping list.
  • Objects โ€” A box of boxes โ€” a name paired with a set of properties. {name: "Sarah", age: 28, active: true}.

Different types in JavaScript

let name = "Sarah"          // Text (String)
let age = 28               // Number (Integer)
let isLoggedIn = true      // Boolean
let colours = ["red", "blue", "green"]  // Array (list)
let user = {               // Object (box of boxes)
  name: "Sarah",
  age: 28,
  active: true
}
โ†’

Try this

Think of a person you know. What information would you store about them in a contact book? Write out 5 pieces of information (name, phone, age, city, active member: yes/no) and decide what TYPE each one is. That's how a database thinks about a person.

RadarTrek Intel โ€” monthly score updates

We track 40+ tools so you don't have to. Score changes, new tools, and new guides โ€” once a month, no spam.