What a program is, what the processor and memory do with it, and how your code travels from text through a TypeScript check to a running result. You run your first lines of TypeScript in the browser, see why the order of lines matters, and fix the two mistakes every beginner meets first.
Fundamentals
TypeScript
What a program is and how a computer runs it. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
This morning your phone woke you up. You set that alarm days ago
and forgot about it, but the phone did not: it kept checking the
time, and at 7:00 it rang. You pressed snooze, and a few minutes
later it rang again. Nobody at the phone company was watching your
alarm. Somebody, years ago, wrote down exactly what a phone should
do when an alarm is due and when someone presses snooze, and your
phone has been following those instructions ever since.
Those written-down instructions are a program. So is the map that
sent you around a traffic jam, and the bank app that refused your
password because Caps Lock was on. You use dozens of programs
every day without seeing one. In this lesson you look inside: what
a program actually is, what a computer does with it, line by line,
and how to run your first lines of TypeScript yourself, right here
on this page, without installing anything.
What a program is
A program is a list of instructions written so precisely that a
machine can follow them without asking you anything. Each line of
that list is an instruction: one small thing to do, like "add
these two numbers" or "show this text on the screen".
The word that matters is precisely. Hand a friend your shopping
list and they cope with everything you forgot to say. A computer
copes with nothing you forgot to say.
list.txt
MilkBreadEgs
Read as eggs
A friend with your list
Does: Fills the gaps with common sense.
"Milk" means the kind you always buy. If the shop is out, they
text you. A typo is read as what you meant.
A computer with your program
Does: Does exactly what is written, nothing more.
It never guesses what you meant. A typo is an instruction it
cannot follow, so it stops and tells you which line.
The same list, read by a person and by a computer
That sounds like a weakness, and it is the whole reason computers
are useful. Something that never guesses also never gets bored,
never skips a step on the four-hundredth repetition, and does the
same thing every Monday for ten years. Your job as a programmer is
to say exactly what you mean. The computer's job is to do it,
fast.
One small step at a time
Inside every computer are two parts you need to know about now.
The processor (often called the CPU, short for central
processing unit) is the part that carries out instructions. It
can only do very small things: add two numbers, compare two
numbers, copy a number from one place to another. What makes it
powerful is speed, not cleverness. It does billions of those small
things every second.
Memory is where the computer keeps the values it is working
with while your program runs. Picture a wall of boxes, each with a
label. A program can put a value in a box, read it back later by
its label, and use it in the next step. When the program ends, the
boxes are emptied.
A program runs top to bottom, one line at a time. The computer
finishes a line completely before it starts the next one.
Here is that happening to four lines of TypeScript. Step through
it with Next step and watch the boxes fill.
The word const makes a new labelled box and puts a value in it.
You will learn boxes properly in the next module; for now, read
const left = total - done; as "work out total - done and keep
the answer in a box called left". The ; at the end marks where
an instruction ends; you will see it on most lines.
What the processor's own instructions look like
Line 3 looks like one instruction to you. To the processor it is
several, each tiny:
Real processor instructions are numbers, not words, and almost
nobody writes them by hand anymore. That is exactly why
programming languages exist, which is the next section.
From your text to a running program
You do not write in the processor's tiny steps. You write in a
programming language: a notation that people can read and
that a program can translate, without guessing, into the steps a
processor understands. What you write in it is called source
code, or just code.
This course uses TypeScript. It is built on top of
JavaScript, the language every web browser runs, and adds one
thing: a checker. Before you run a file, TypeScript can read all
of it and check it for mistakes. So your code can pass through two
tools on its way to the processor: TypeScript, which checks it,
and Node, a program that runs it.
The figure follows monday.ts through both tools, as it goes on
your own computer when you check it first: checked, then run. In a
moment you run the same three lines here. In the version with a
typo, the quotes around Groceries are missing. Press Next step
to follow it, or let it play, and switch between the two versions to
see what one typo changes.
That middle step is why this course teaches TypeScript rather than
plain JavaScript. Plain JavaScript finds most mistakes only when
the broken line is reached, after everything above it has already
happened. TypeScript finds many of them before the first line
runs, while fixing them is still cheap.
Your first lines of TypeScript
Press Run under the code below. The page checks it, runs it,
and shows you what it printed.
console.log shows one line of text in the output. The text goes
between the parentheses, inside double quotes. Three instructions,
three lines, in exactly the order you wrote them.
The quotes are not decoration. They tell the computer "this is
text, show it exactly as written". Without them, a word is read
as the label of a box, which TypeScript calls the box's name,
and the computer goes looking for that box.
Bad — the quotes are missing, so
Groceries is read as the name of a box nobody made.
Good — in quotes, Groceries is
text to show.
Run the first one. TypeScript stops it before it starts and
reports Cannot find name 'Groceries'. Notice that not even
Monday list is printed: the check happens before any line runs.
A friend would have known you meant the word. The computer has
only what you wrote.
Now run something where the computer does the arithmetic. Press
Step instead of Run to watch it line by line, the way the
four-line walkthrough above showed you.
console.log can take more than one value, separated by commas.
It shows them on one line with a space between, so this prints
Still to buy: 3.
Predict before you run
Reading a program and saying what it will do, before you run it,
is the most useful habit in this whole course. Make a guess, then
run it to see whether you were right.
Predict
What is the second line this program prints?
The common mistake: using a box too early
Because a program runs top to bottom, a box exists only from the
line that makes it. Beginners often write the line they care
about first, the result, and the lines that work it out below.
Common mistake
Using a value before the line that makes it
You want to see how many tasks are left, so you write that line
first. To you, the answer is right there on the page.
On line 1, the box left does not exist yet: it is made only on
line 4, and a running program never waits for a box made further
down. When TypeScript checks the file, as Run does here, it
reads all of it first, sees that order, and refuses it before
anything runs. In TypeScript's words, a box is a variable, and
the line that makes it is its declaration. The word
block-scoped in the message is explained in the next module.
Block-scoped variable 'left' used before its declaration.
The fix
Put the lines in the order the work has to happen: make the
boxes, then use them.
TypeScript often says it twice, in two wordings: the message above
and Variable 'left' is used before being assigned. Both mean the
same thing: the box is used above the line that makes it.
When an error mentions a name, ask yourself one question: on the
line that uses it, has that name been made yet? Read upward from
the line the error points to, not downward.
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. Pick an answer, or press
Check when your code is ready; if you are stuck, the hints come
one at a time.
Recap
Recap · 3 questions
Check yourself
What is a program?
Check yourself
In what order does the computer carry out your lines?
Check yourself
What does TypeScript do before your program runs?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
What a program is and how a computer runs it
What things are
Word
What it means
Program
Instructions precise enough to need no guessing
Instruction
One small thing to do: add, compare, show text
Processor
The CPU, which carries instructions out, fast
Memory
Labelled boxes holding values while a program runs
Source code
What you write, in a programming language
TypeScript
JavaScript plus a checker that finds mistakes first
Node
The program that runs your code on your computer
Code
Write
To
console.log("Hi")
Show one line of text
console.log("Left:", 3)
Show several values on one line
const left = 5 - 2;
Work it out, keep it in a box called left
How it runs
Rule
What it means
Top to bottom
One line finishes before the next starts
Checked first
On lesson pages, a TypeScript error means no line runs
Boxes first
Make a box above the line that uses it
Quotes matter
"Groceries" is text; Groceries is a box's name
Where to go next
You now know what a program is, what the processor and memory do
with it, how your code travels from text through TypeScript and
Node to a result, and why the order of your lines matters. Every
lesson in this course builds on those four ideas.
So far the page has done the running for you. Next is Your
setup: VS Code, Node.js, and basic terminal commands, where you
install VS Code (Visual Studio Code, the editor you write code in)
and Node.js (Node's full name) on your own computer, so the
programs you write are files you keep and can run whenever you
like.
Before you move on, go back to monday.ts in this lesson, press
Edit, and turn it into your own to-do list for tomorrow. Add a
line that counts your tasks and prints how many are left. Predict
the output before you press Run, every time.
Yourcode
TypeScript
Node
Screen
Your code: monday.tsmonday.tsmonday.ts
TypeScript: Cannot find name 'Groceries'.Cannotfindname'Groceries'.Cannotfindname'Groceries'.
A file of characters you typed. Nothing has happened yet: to the
computer, it is still only text.
TypeScript reads every line before any of them runs. This one
cannot work, so it stops here and names the line.
Node never starts, so not one line of your program runs.
Nothing reaches the screen.
Your code is checked first, and only checked code is run
All 4 steps shown.Step 1 of 4: Your code: monday.tsStep 2 of 4: TypeScript: Cannot find name 'Groceries'.Step 3 of 4: Node: never startsStep 4 of 4: Screen: nothing printed
All 4 steps shown.
Yourcode
TypeScript
Node
Screen
Your code: monday.tsmonday.tsmonday.ts
TypeScript: checked ✓checked✓checked✓
Node: runs lines 1–3runslines1–3runslines1–3
Screen: Monday list…Mondaylist…Mondaylist…
Time
A file of characters you typed. Nothing has happened yet: to the
computer, it is still only text.
TypeScript reads every line before any of them runs, and finds
nothing wrong.
A program called Node carries your instructions out, top to
bottom, handing each one to the processor as tiny steps.
Text on the screen, a file saved, a message sent: whatever your
instructions asked for.
Your code is checked first, and only checked code is run
All 4 steps shown.Step 1 of 4: Your code: monday.tsStep 2 of 4: TypeScript: checked ✓Step 3 of 4: Node: runs lines 1–3Step 4 of 4: Screen: Monday list…
All 4 steps shown.
Exercise · Read the code
Read what is in the box
Once line 3 has run, what is in the box called left?
ts
const total = 6;const done = 4;const left = total - done;console.log(left);
list.txt
MilkBreadEgs
Stops here: line 3
text
load the value from box "total" → 5load the value from box "done" → 2subtract the second from the first → 3store the result in box "left"
console.log("Monday list");const total = 9;const done = 5;console.log("Still to do:", total - done);console.log("Good luck");
ts
console.log("Tasks left:", left);const total = 5;const done = 2;const left = total - done;
ts
const total = 5;const done = 2;const left = total - done;console.log("Tasks left:", left);
CodeThe program, line by line. The marked line is the one the computer is running now; the lines above it have run.ts
ts
const total = 5;const done = 2;const left = total - done;console.log("Tasks left:", left);
MemoryThe boxes in memory, each with its name. A box appears when a line makes it; the one just filled or changed is orange.
total55
done22
left33
OutputEverything the program has printed so far.
Nothing printed yetNothing printed yet
Every step as text
Line 1.total = 5.
A box labelled total is made, and 5 is put in it.
Line 2.total = 5, done = 2.
A second box, done, holding 2. The first box is untouched.
Line 3.total = 5, done = 2, left = 3.
The computer reads total and done, subtracts, and puts the
result, 3, in a new box called left.
Line 4.total = 5, done = 2, left = 3.
console.log shows its two values on one line: Tasks left: 3.
There are no more lines, so the program ends.
Prints Tasks left: 3
Four lines, carried out in order, top to bottom
Exercise · Fix the error
Put the quotes back
This list should print three lines, but TypeScript finds a
mistake, so the page will not run it. Fix it so it prints
Tasks for today:, Groceries and Laundry, in that order.
Loading the editor…
Exercise · Fix the error
Make the boxes before using them
This program should print Done so far: 3 and then
Still to do: 4. Change the order of the lines, without changing
any line itself, so it runs.
Loading the editor…
Exercise · Write the code
Write your morning as a program
Write a program that prints these three lines, exactly, in this
order: