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.
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:
- Numbers —
42,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/falsein JS,True/Falsein Python). Fuel for the conditions in the next lesson.
The classic trap: number or string?
"10" in quotes is a string, 10 without quotes is a number. They look alike, they behave differently:
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.
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"and10are different things; weird arithmetic = check your types.- Variable names describe the contents.