Variables & Numbers

The Course
•
5 min read

A value is a piece of data: 20, "reading", or later a list. A variable gives a value a name so you can refer to it again. const minutes = 20; creates a name and assigns its initial value. It does not print anything by itself.

Use const when you will not reassign the name. Use let when the value needs to change. = assigns; it is not a mathematical statement of equality. minutes = minutes + 5 reads the old number, adds five, and stores the result. Avoid var in this course; its older scope rules add a distraction we do not need.

Numbers support +, -, *, and /. Parentheses control grouping: (10 + 5) * 2 is 30. Choose names that say what the number measures: minutesPerSession is clearer than x. JavaScript names often use camelCase: a lowercase first word, then capital letters at word boundaries.

In console.log(planned), the unquoted name asks JavaScript to look up the stored value. console.log("planned") would print the word instead. An expression is code that produces a value, such as planned - 15. You can pass an expression directly to console.log or save its result in a variable.

Variable names cannot contain spaces or start with a digit. Use letters and digits with a lowercase first letter for the names you write here. Spaces around operators make code easier to read: remaining - 15 works the same as remaining-15.

Predict before running

Track the value of remaining after each assignment. Does the original planned value change? After running, modify the completed amount from 15 to 25.

Predict, then run JavaScript
const planned = 60;
let remaining = planned;

remaining = remaining - 15;
console.log(remaining);
console.log(planned);
Check your prediction
45
60

Trace each statement in order. If your prediction differed, describe the first place your mental model and the result diverged before trying again.

Calculate first, format later

Operators follow an order: multiplication and division happen before addition and subtraction. Parentheses make a different grouping explicit. Do not rely on remembering precedence when a pair of parentheses would make your intention easier to read.

A variable stores the result calculated at that moment. It is not a spreadsheet formula that updates itself whenever a different value changes. Trace the assignment to total before changing sessions.

Calculate first, format later JavaScript
let sessions = 2;
const total = sessions * 25;

sessions = 3;
console.log(total);
console.log(sessions * 25);
console.log(10 + 5 * 2);
console.log((10 + 5) * 2);
Check your prediction
50
75
20
30

total remains 50 because its expression ran when sessions was 2. Computing the expression again uses 3.

Choosing a declaration

NeedUseExample
A fixed goalconstconst goal = 60;
A running totalletlet completed = 0;
A revised totalassignmentcompleted = completed + 20;

Avoid declaring the same name twice in the same program block (a group of instructions). We will study these boundaries, called scope, on Day 4. To update a let variable, omit let the second time. Prefer names with units: durationMinutes, not a number whose meaning you will forget tomorrow. JavaScript distinguishes uppercase and lowercase letters; goal and Goal are different names.

Try it: in one editor, write const goal = 60; followed by goal = 90;. Predict whether that reassignment should succeed. Run it, read the error, and remove the second line. The lesson is to choose the declaration that matches your intention—not to change every const into let to silence an error.

Your turn

Exercise

Variables & Numbers: practise

You have 3 sessions of 25 minutes. Calculate the total and subtract a 10-minute interruption. Print 65 using named variables, not just the answer.

Your working space JavaScript
const sessions = 3;
const minutesPerSession = 25;
// Calculate and print the available study minutes.
Hint 1: the idea

Multiplication finds the total; subtraction accounts for the interruption.

Hint 2: a first step

Store sessions * minutesPerSession in a let variable before subtracting 10.

Show one solution
const sessions = 3;
const minutesPerSession = 25;
let total = sessions * minutesPerSession;

total = total - 10;
console.log(total);
65

A name should communicate meaning, not just shorten typing. Explain why total uses let here, and how you could instead express the calculation with one const.

What you should be able to explain

  • Why does changing sessions not recalculate total?
  • When should you use let instead of const?
  • Show how parentheses change a calculation without changing its numbers.

Was this article helpful?

Your response is saved on this device.