M Logo
๐Ÿ“ TypeScript from Zero ยท Part 2 of 50

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.

We stay in the same hello-ts project and add a file:

bash
cd hello-ts
touch variables.ts
code .

1. let: a value that can change

let, a name, =, and a value:

ts
let score = 10;
console.log(score);
score = 15;
console.log(score);
bash
node variables.ts
text
10
15

2. const: a value that stays

For values that should never change, use const (constant):

ts
const playerName = "Ana";
console.log(playerName);
text
10
15
Ana

3. Changing a const: crash and error

Add a line that assigns to the constant:

ts
playerName = "Ben";

Node stops while running:

text
TypeError: Assignment to constant variable.
    at Object.<anonymous> (file:///home/mah/hello-ts/variables.ts:7:12)

tsc finds it without running anything:

bash
npx tsc --noEmit variables.ts
text
variables.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:

ts
score = "ten";
console.log(score);

Node does not care and prints ten. But tsc complains:

text
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: score is not Score.
  • By habit: first word small, next words with a capital (camelCase), like playerName.

Final code

ts
let score = 10;
console.log(score);
score = 15;
console.log(score);
const playerName = "Ana";
console.log(playerName);

Recap

  • let for values that change, const for values that stay.
  • TypeScript remembers each variable's type (TS2322, TS2588).

Next episode: Numbers and Math.

ยฉ 2026 All Rights Reserved By Mahmudul Alam