Imagine your bag before a school trip: a packed lunch, a
Values in a program are exactly like that. Every value has a type — a label that says what kind of thing it is. And the type isn't just a name: it decides what operations make sense for that value. This one idea — that a value's type tells you what you're allowed to do with it — is at the heart of every program ever written.
For a beginner, three types cover almost everything you'll meet:
"hello" or
"Ada Lovelace" (the quote marks are how you know it's a string);true or
false, and nothing else.
Here is a tiny program that makes one value of each type and prints it out. The special word
typeof asks TypeScript "what type is this?" — press Run and read
the Output tab to see it name each one.
The Output shows number, string and boolean — the three
core types, each doing its own job.
This is the big idea, so let's see it clearly. The + symbol means
two different things depending on the type of value on each side. With
numbers, + does arithmetic — it adds. With
strings, + joins the text end to end (programmers call
this concatenation).
You can multiply numbers but not text — "cat" * 2 is meaningless. You can
ask a string for its length or make it upper-case, but those questions make no sense for a plain
number. The type is what tells the computer (and you) which operations are allowed.
+, measure with
.length, change case, search inside it.if decisions.
A boolean can only ever be true or false — there is no
in-between. You rarely type true yourself; more often a boolean is the
answer to a question, produced by a comparison like "is this bigger than that?". Those
answers are what an if statement uses to decide what to do.
Notice that age >= 13 isn't a number and isn't text — it's a boolean, the type
whose whole job is to answer yes/no questions.
So far TypeScript has worked out each type for us. But we can also say the type out
loud by writing a colon and the type name after the variable: : number,
: string or : boolean. This is called a type annotation.
Why bother, if TypeScript can already tell? Because writing the type turns it into a promise the computer will check for you. If you later try to put the wrong kind of value into that box, TypeScript complains before the program ever runs — catching a whole class of mistakes early. It's a bit like labelling a jar "sugar": anyone (including future-you) instantly knows what belongs inside.
You can convert between types on purpose using the type's name as a function.
String(7) turns the number "7", and Number("7") turns the text back into the number. This is
different from the accidental mixing in the "Watch out!" below — here you are the one
choosing to change the type, so there's no surprise.
Here is the classic beginner trap. The text "5" (a string, with
quote marks) is not the same as the number 5 — even though they
look alike on screen. Because the type is different, the + operator behaves
differently, and the results are worlds apart:
When one side of + is a string, the computer turns the other side into text too
and joins them — so "5" + 3 becomes the text "53", not
Number("5") +
3 gives the