Values and variables
Names for values: const for the ones that stay, let for the ones that change, and building sentences with template literals.
Names for values
A variable is a name for a value. const name = "Ada"; gives the value a name you can use afterwards — and const says the name will not be pointed somewhere else later, which is the safe default.
Use let when the value genuinely changes as the program runs: a counter, a total, something a click updates. That is the whole decision, and reaching for const first means the program has fewer moving parts to reason about.
The declaration is also a promise to the reader. const says this name is fixed from here on, so a search for its other uses is short; let says it will move, so the places that change it are worth finding. A file where most names are const is a file with fewer surprises.
const, let, and a sentence built from parts
const name = "Ada";
let serves = 0;
serves = serves + 1;
console.log(`${name} served ${serves} loaf`);The kinds of value
Text goes in quotes — single, double, either — and is called a string. Numbers are written bare: 36, 1.5. Booleans are true and false, and they are what comparisons produce.
A value does not change its type by being printed: "36" is text and 36 is a number, exactly as in Python. Adding two strings joins them; adding a string and a number converts the number to text rather than refusing, which is convenient and occasionally surprising.
The type is a property of the value, not of the name. A let can hold a number on one line and a string on the next, which is legal and rarely wise. Keeping one kind of value under one name is what makes typeof checks and arithmetic predictable.
Template literals
A template literal is text with holes in it: a backtick opens and closes the string, and ${…} marks wherever a value is filled in — Hello, ${name}! reads as a sentence and does not need the plus signs of older JavaScript.
This is the same idea as an f-string in Python, down to the dollar sign. Anything inside the braces is evaluated first — a name, a sum, a function call — and the result lands in the sentence.
A name is a binding, not a box
It helps to read const name = "Ada"; as "this name now refers to that value", not as a box the value sits inside. const stops the name being pointed somewhere else; it says nothing about the value itself. A const array can still be pushed into, and a const object can still have a field changed.
That is why the safe default is still const: most values are never reassigned even when their contents change. Reach for let when the name itself has to move — a counter, a total, a value replaced on each click.
Text, numbers, and the quiet conversion
The plus sign has two jobs. When both sides are numbers it adds; when either side is text it joins, converting the other side first. So 2 + 2 is 4, but "2" + 2 is "22". The other arithmetic operators always work on numbers, which is why multiplying the text "6" by 2 gives 12 rather than a complaint.
Number("42") makes the conversion explicit and returns NaN when the text is not a number at all. typeof tells you which kind you have before the arithmetic surprises you — and a value from an input field is always text, however numeric it looks.
A worked conversion
Suppose a form field holds "5" and you want one more. input.value + 1 gives "51", because the plus sign sees text and joins. Number(input.value) + 1 gives 6, because the conversion happens first and both sides are now numbers.
The same distinction runs through every calculation in the course. Multiplying the text "3.5" by 2 happens to give 7, since the star has no text meaning and converts its operands, but relying on that breaks the moment the text is not a number. Convert once, at the edge where the text enters the program, and keep the arithmetic on numbers.
Three traps worth naming: const does not freeze an array or object, so const list = []; list.push(1) is legal; a field’s .value is text, so "5" + 1 gives "51" rather than 6 — convert with Number() first; and undefined (never assigned) and null (deliberately empty) are different from 0 and from "".
Try these next
- Log
typeoffor a string, a number and a boolean, and read the three answers. - Convert
"42"withNumber()and add one to it; then try the same without the conversion. - Reassign a name declared with
constand read the error. It points at the assignment, not the declaration.
From values to a sentence
Declare a name and a number of loaves, then log one sentence that includes both — built with a template literal, not with plus signs.
3 left to do
- A value is declared with const
- The sentence is a template literal
- The sentence is logged
A backtick opens and closes the sentence, and ${…} marks each hole: ${name} baked ${loaves} loaves.
A receipt with two decimals
A loaf costs 3.50 and you buy 4. Store the price and the quantity, compute the total, and log one sentence that shows it with exactly two decimal places.
3 left to do
- The total is computed from the values
- The total is shown to two decimals
- The sentence is logged
const total = price * quantity; then ${total.toFixed(2)} inside a template literal. toFixed(2) turns the number into text with two digits after the point.
A name that cannot be reassigned is a promise the rest of the file can rely on. const first, let when you must, and never a bare assignment to a name you have not declared.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
When should you reach for let instead of const?
