Course
BASICModule 1: How Programs Think· 2/3

Variables & Types

Boxes with labels

A program works with data: numbers, text, lists. To use data, you need to hold it somewhere — that's what variables are for. A variable is a box with a label: you put a value in, you refer to it by name.

PLAYGROUND

The language difference is minimal: JavaScript declares a variable with the word let (or const — for ones that won't change), Python just with a name. You choose the variable name; a good name describes the contents: price, userName, totalSum — not x, a1, qwe.

Data types

Values come in different types, and the type determines what you can do with a value:

  • Numbers42, 3.14, -500. You can add, multiply, compare them.
  • Strings — any text in quotes: "hello", "QBT-PRO-1234". Joined, sliced, searched.
  • Booleans — only two values: true/false (true/false in JS, True/False in Python). Fuel for the conditions in the next lesson.
PLAYGROUND

The classic trap: number or string?

"10" in quotes is a string, 10 without quotes is a number. They look alike, they behave differently:

PLAYGROUND

This trap is a source of real bugs: data from a file or a website almost always arrives as strings, and "adding" "100" + "200" gives "100200" instead of 300. Type conversion is the first thing to check when arithmetic acts strange.

Exercise

Below is a starter in the playground. Make the program compute the cost of an order: price per unit × quantity, and print the result as a sentence. Test yourself by changing the values.

PLAYGROUND

Key takeaways

  • A variable is a named box; a new value evicts the old one.
  • Basic types: numbers, strings, booleans; the type determines the available operations.
  • "10" and 10 are different things; weird arithmetic = check your types.
  • Variable names describe the contents.
CHECK YOURSELF
1. What does adding strings "5" + "5" print in JavaScript?
2. Which variable name is best for holding the number of items in a cart?
3. Data came from a file, and 100 + 200 gives 100200. What is happening?
What Is CodeConditions & Logic