Name a value with const when it is filled once and with let when a later line refills it. Read = as an order, not a comparison: the right side is worked out first. See why TypeScript refuses a refilled const before anything runs, and make TaskKit count the tasks it adds.
Fundamentals
TypeScript
`const` and `let`. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
At the end of the last module TaskKit's banner built its line from
a single character: const line = "=".repeat(28);. You have
written const in every lesson since the first one, and read it
as "make a box and put this value in it". It has worked every
time, because every box so far was filled once and only read
after that.
Now TaskKit should count the tasks it adds. Think of the
scoreboard at a football match. The team names are painted on
once, before kick-off, and nobody touches them. The score is
changed by hand every time a goal goes in. A program needs both
kinds of value, and TypeScript has a word for each: const for
the painted names, let for the score. By the end of this lesson
you will know which box locks and which can be refilled, how to
read the = in added = added + 1, and TaskKit will tell you how
many tasks it has added.
A variable is a labelled box
A program works with values: a version number, the name of a task,
how many tasks are done. To use a value more than once, you keep it
in memory under a name, in one of the labelled boxes from the first
lesson. A named place in memory that holds a value is called a
variable: the name is on the label, the value is inside.
The line that makes a variable is a declaration. Take the
first line of TaskKit's banner apart:
ts
const version = "0.1";
const is a keyword: a word with a fixed meaning in the
language, which you cannot use as a name. It says what kind of
box to make.
version is the name, written on the label. You choose it.
= puts the value on its right into the box.
"0.1" is the value that goes in.
; ends the instruction, as in the first lesson.
That = is the part to read carefully. In maths, = says that
two sides are equal. In code it is an order: work out the right
side, then put the result into the box named on the left. Putting
a value into a box is called assignment, and = is the sign
for it. You will compare values for equality in two
lessons' time, with a different sign.
Two kinds of box
Some values are worked out once and never change while the
program runs: TaskKit's version, the banner's line, the name of a
task you just added. Others follow what the program does: how many
tasks have been added so far goes up every time one is.
TypeScript gives each its own kind of box. A box made with
const, short for constant, is locked as soon as it is filled.
A box made with let can be refilled: a later line can put a
new value in it, and the old one is gone. Here are two of the
boxes the last module left in TaskKit, version and task,
beside a counter that has just gone from 0 to 1. Press Next variable to
walk through them, or let it play:
Variables
const version holds "0.1"constversion"0.1"
const task holds "Buy milk"consttask"Buy milk"
let added holds 1, was 0letadded0→1
Every value lives in a labelled box; a const box is locked
All 3 variables shown.Variable 1 of 3: const version holds "0.1"Variable 2 of 3: const task holds "Buy milk"Variable 3 of 3: let added holds 1, was 0
All 3 variables shown.
Two of the three boxes are locked, and that is typical. In most
programs nearly every value is filled once, and only a few boxes are
counters or totals that change. The lock tells you, and everyone who
reads your code after you, that version holds "0.1" on every
line after the one that makes it, so nobody has to search the file
to find out whether it changed.
const: filled once
Everything you have written so far was const, and it was the
right choice every time: the banner's boxes are filled once and then
only read. Because a const box can never be refilled, it has to be
filled on the line that makes it. Run this, and TypeScript refuses
the first line:
The message that matters is 'const' declarations must be initialized. Here, initialized means given its first value. The
other message, about an any type, comes from the same missing
value; any comes much later, in Module 8.
Try to put a second value into a locked box, and the check refuses
the file before a single line runs. You will see that message, and
what Node does when nobody checks, in the common mistake later in
this lesson.
let: a box you can refill
A count of added tasks starts at 0 and goes up by one each time a
task is added. That is a box you refill, so it is made with let:
To refill it, write the name, = and the new value, with no let
in front. let makes a box; a line without it uses the box that
is already there. The line you will write most often looks
strange the first time:
In maths this is nonsense: no number equals itself plus one. Read
it as the order it is. The right side is worked out first, using
the value that is in the box now. Only then does the result go
into the box, and the old value is gone. Press Next step to
watch the box change, line by line:
Now run one yourself. Press Step instead of Run to see
that each console.log prints what is in the box at that moment,
not what it will hold at the end:
It prints Tasks added: 0, then Tasks added: 1. The first
console.log ran before the refill, so it saw the 0.
Writing let again on a refill is the most common slip with
let.
Bad — let on the refill asks
for a second box with a name that is already taken.
Good — no let on the refill,
so the one box is refilled.
Run the first one. TypeScript refuses it with TS2451, Cannot redeclare block-scoped variable 'added'., and names both lines that make a
box called added. To redeclare is to declare the same name a
second time, and a block-scoped variable is TypeScript's name
for a box made with let or const. One name, one box: make it
once, refill it as often as you like.
Which one to choose
Both kinds hold a value under a name. The difference is only what
may happen to the box afterwards:
const
Does:
Keeps the value it was made with, from its line to the last
Never: It never takes a second value
let
Does:
Takes a new value every time a line refills it
Never: A refill never repeats the word let
Start with const; switch to let only for a box you refill
So the rule is short. Make every box with const. Change it to
let only when you write a line that refills it. If you write a
refill and forget to switch to let, TypeScript tells you at once,
and you change one word. Going the other way, with let everywhere
"just in case", costs you the lock: TypeScript no longer catches a
refill you did not mean.
TaskKit counts its tasks
At the end of the last module, TaskKit printed its banner and
nothing else. Open index.ts in ~/projects/taskkit and add the
lines under the banner, so the file reads as below. If you kept the
two lines from lesson 1.6's common mistake under the banner, delete
them: the new lines take their place. If you made the banner print
TASKKIT 0.1 with toUpperCase(), keep your version of that line
(your output's second line then reads TASKKIT 0.1).
The empty lines change nothing for the computer; they group the
lines for you. Two tasks need two boxes, so task became
firstTask and secondTask: a name cannot hold a space, so the two
words are joined; by habit the second starts with a capital letter,
as lesson 2.7 explains. You
could refill one let task box instead, but then the first name
would be gone once the second went in; two const boxes keep both
names, each filled once. Only added is let, because it goes up
each time a task is added. Check the file with tsc (short for
TypeScript compiler), which npx starts from your taskkit
folder, then run it:
The check prints nothing, which means it found nothing wrong. The
task names are still typed into the code; by the end of this
module, TaskKit will read them from the command line instead.
Predict before you run
Line 2 makes a box from what is in another box. Make a guess, then
run it.
Predict
What does this program print?
The common mistake: refilling a const box
Suppose the next version of TaskKit is ready, and suppose
TaskKit's index.ts were only four lines. You add a line that puts
"0.2" into the box, rather than change the line that made it.
Common mistake
Putting a second value into a const box
The file:
npx tsc --noEmit index.ts refuses line 3: version was made with
const, so it is locked.
Cannot assign to 'version' because it is a constant.
The fix
Ask whether the value really changes while the program runs. A
version does not: it is one value for the whole run. So change it
where it is made, and keep the lock:
The box is locked, so line 3 cannot work. What happens next depends
on whether anything checks the file first. Without a check, Node
stops with a TypeError: Node's name for a value used in a way it
does not allow. Press Next step to follow it, or let it play,
and switch between the two ways of running it:
This is why TypeScript cares which kind of box you chose. When you
see TS2588, ask the Fix's question: does the value really change
while the program runs? If it does, like a count, the box should be
let: change the word on the line that makes it. If it does not,
the refill is the bug: change the value where the box is made, and
delete the line that refills it.
Pass all four tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
Each exercise is checked in your browser. Two ask you to read code,
one to fix it and one to write it; try the last one in your own
index.ts too.
Recap
Recap · 3 questions
Check yourself
What does const promise about a box?
Check yourself
What does added = added + 1 do?
Check yourself
What do you lose by making every box with let?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
const and let
What things are
Word
What it means
Variable
A named box in memory that holds a value
Declaration
The line that makes a box: a keyword, a name, a value
Keyword
A word the language reserves, like const and let
Assignment
Putting a value into a box with =
const
A box filled once, then locked
let
A box that later lines can refill
Code
Write
To
const version = "0.1";
Make a locked box, filled on the same line
let added = 0;
Make a box you will refill
added = added + 1;
Refill it: the right side is worked out first
added = 5;
Refill it, with no let in front
Rules
Rule
What it means
const first
Switch to let only when a line refills the box
One name, one box
let or const once per name
A box holds a value
Copying a number or text copies the value
TS2588
A const box was refilled: make it let, or drop the refill
TS2451
A name was declared twice: drop the second let
var
Older code; read it as let, never write it
Where to go next
You can now make both kinds of box, read = as "work out the
right side, then put it in the box", refill a let box, and tell
from the first line of an error whether you refilled a locked box
or made one twice. TaskKit counts the tasks it adds.
Next is Basic types: string, number, boolean. Every box
you made today holds either text or a number, and the next lesson
names those kinds of value, and shows why TypeScript will not let
a box made for a number be refilled with text.
Before you move on, make the common mistake on purpose. Add
version = "0.2"; under the banner in index.ts, check the file
and read the message, then run it with node alone and see which
lines print before it stops. Delete the line, and check once more
before you call it done.
Line 1 makes const version; line 3 tries to refill it with
"0.2".
Cannot assign to 'version' because it is a constant. It
names line 3, and nothing runs.
The check failed, so you fix line 3 before you run anything.
Nothing reaches the screen.
A refill of a const box: caught before running, or halfway through
All 4 steps shown.Step 1 of 4: Your code: index.tsStep 2 of 4: TypeScript: TS2588Step 3 of 4: Node: never startsStep 4 of 4: Screen: nothing printed
All 4 steps shown.
Yourcode
TypeScript
Node
Screen
Your code: index.tsindex.tsindex.ts
TypeScript: never askedneveraskedneverasked
Node: lines 1–2lines1–2lines1–2
Screen: TaskKit 0.1TaskKit0.1TaskKit0.1
Node: TypeErrorTypeErrorTypeError
Screen: line 4 lostline4lostline4lost
Time
Line 1 makes const version; line 3 tries to refill it with
"0.2".
You ran node index.ts straight away, so nothing checked
the file.
Node fills the box and prints the first line.
The first line is printed before anything goes wrong.
Line 3: TypeError: Assignment to constant variable. Node
stops here.
Line 4 never runs: half a program ran, then it stopped.
A refill of a const box: caught before running, or halfway through
All 6 steps shown.Step 1 of 6: Your code: index.tsStep 2 of 6: TypeScript: never askedStep 3 of 6: Node: lines 1–2Step 4 of 6: Screen: TaskKit 0.1Step 5 of 6: Node: TypeErrorStep 6 of 6: Screen: line 4 lost