Skip to content

Programming Basics

Variables & Data Types

The idea, in plain English

A variable is a labeled box. You can put something inside it. Think of a box labeled 'Snacks'. It holds chips today and cookies tomorrow. The label — the variable's name — stays the same. What's inside — the value — can change. A 'data type' is the KIND of thing inside the box. It could be a number, a piece of text (called a 'string'), or a yes/no answer (called a 'boolean').

How it works

  1. 1Pick a name for your box, like `age`. Put something inside it using `=`. This is called 'assigning' a value.
  2. 2Type numbers with no quotes, like `12`. Put quotes around text to make it a 'string', like `"Ava"`. Yes/no values are called 'booleans'. They are just `true` or `false`.
  3. 3Use the variable later by writing its name. The computer looks inside the box. It hands you back whatever is stored there.

When you'd use it

Use a variable anytime you need to remember something for later. This could be a user's name, a score, or whether a checkbox is ticked. Almost every line of code you write uses a variable.

Common beginner mistakes

  • Forgetting quotes around text. `age = Ava` confuses the computer. It needs `age = "Ava"`.
  • Mixing up types, like doing math on text. `"12" + 1` does not add numbers the way you expect in every language.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
Name: Ava
Age: 12
Height: 1.50
Is student: yes

Not sure this is the right topic? See the learning paths → or where this leads →