Data Types

Imagine your bag before a school trip: a packed lunch, a \pounds 5 note, and a permission slip that's either signed or not signed. Each of those is a completely different kind of thing. You can eat the lunch, you can spend the money, and you can check whether the slip is signed — but it would be nonsense to try to eat the money or spend the lunch.

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:

The three types, all at once

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.

const score = 42; // a number const player = "Priya"; // a string (text — note the quotes) const hasWon = false; // a boolean (true or false) console.log(score, "is a", typeof score); console.log(player, "is a", typeof player); console.log(hasWon, "is a", typeof hasWon);

The Output shows number, string and boolean — the three core types, each doing its own job.

The type decides what operations make sense

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).

// + on numbers ADDS them console.log(10 + 5); // 15 // + on strings JOINS them console.log("foot" + "ball"); // "football" console.log("Hi " + "there"); // "Hi there" // Some operations only make sense for one type: console.log(6 * 7); // multiplying numbers → 42 console.log("ha".repeat(3)); // repeating a string → "hahaha"

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.

Booleans: the yes/no type

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.

const age = 13; const isTeenager = age >= 13; // a comparison PRODUCES a boolean console.log("Teenager?", isTeenager); // true const score = 68; const passed = score >= 50; console.log("Passed?", passed); // true if (passed) { console.log("Well done! 🎉"); } else { console.log("Try again."); }

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.

Naming the type in TypeScript

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.

const distanceKm: number = 5.2; const busStop: string = "Market Square"; const raining: boolean = true; console.log("Walking", distanceKm, "km to", busStop); console.log("Bring an umbrella?", raining);

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 into the text "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.

const points: number = 9; // Deliberately build a message by converting the number to a string: const message: string = "You scored " + String(points) + " points"; console.log(message); // And convert text back into a number so we can do maths with it: const typed: string = "20"; console.log(Number(typed) + 5); // 25, not "205"

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:

console.log(5 + 3); // numbers → ADDS → 8 console.log("5" + 3); // string + number → JOINS → "53" console.log("5" + "3"); // two strings → JOINS → "53"

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 8. This bites people constantly when a value comes from somewhere that hands back text (like something a user typed into a box). The fix is to make sure a value is really a number before doing arithmetic — for example Number("5") + 3 gives the 8 you expected. Rule of thumb: if it has quote marks, it's text, and text doesn't do sums.