Variables
6 min · TypeScript
Store values in named boxes — and label what type they hold.
Enemy 1 of 5
A variable is a name that holds a value, so you can refer to it later instead of retyping the value everywhere it's needed. You make one with let, followed by the name, an equals sign, and a value — exactly like plain JavaScript.
TypeScript adds one more piece: after the variable name, you can add a colon and a type, like : string. This is called a type annotation, and it tells the compiler exactly what kind of value this variable is allowed to hold. If you (or a teammate, months later) ever try to assign a number to name, TypeScript will flag it as an error immediately, right in the editor, well before the code ever runs.
Once a value is stored, you use the name anywhere instead of repeating the value — same as JavaScript. The type annotation doesn't change how the program behaves at runtime; it exists purely to help catch mistakes while you're writing the code.
Loading editor...