TypeScript from Zero: Variables with let and const
November 6, 2026
Episode 2 of TypeScript from Zero. In episode 1 we ran our first program. Today: variables. A variable is a name for a value, so the program can remember it. We use let and const, and meet two real errors. All code below is the final code from the video.
- All episodes: TypeScript from Zero playlist.
We stay in the same hello-ts project and add a file:
cd hello-ts
touch variables.ts
code .1. let: a value that can change
let, a name, =, and a value:
let score = 10;
console.log(score);
score = 15;
console.log(score);node variables.ts10
152. const: a value that stays
For values that should never change, use const (constant):
const playerName = "Ana";
console.log(playerName);10
15
Ana3. Changing a const: crash and error
Add a line that assigns to the constant:
playerName = "Ben";Node stops while running:
TypeError: Assignment to constant variable.
at Object.<anonymous> (file:///home/mah/hello-ts/variables.ts:7:12)tsc finds it without running anything:
npx tsc --noEmit variables.tsvariables.ts:7:1 - error TS2588: Cannot assign to 'playerName' because it is a constant.Rule: use const by default, and let only when the value really has to change. Delete the bad line.
4. TypeScript remembers the type
score started as a number, so TypeScript knows it is a number. Put text into it:
score = "ten";
console.log(score);Node does not care and prints ten. But tsc complains:
variables.ts:7:1 - error TS2322: Type 'string' is not assignable to type 'number'.That is the "types" part of TypeScript. Delete those two lines and tsc prints nothing again.
5. Naming rules
- Letters, digits, underscores and
$, but never start with a digit. - Upper and lower case are different names:
scoreis notScore. - By habit: first word small, next words with a capital (camelCase), like
playerName.
Final code
let score = 10;
console.log(score);
score = 15;
console.log(score);
const playerName = "Ana";
console.log(playerName);Recap
letfor values that change,constfor values that stay.- TypeScript remembers each variable's type (
TS2322,TS2588).
Next episode: Numbers and Math.