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

TypeScript Arrays: Index, push, pop and length

November 6, 2026

Welcome to episode 8 of TypeScript from Zero, a series for complete beginners (no JavaScript needed). Welcome back to TypeScript from zero! Today we learn arrays. An array is an ordered list of values. A new file called arrays.ts. Every code block below is the code from the video, ready to copy; run each file to see the output.

Setup

We start in the terminal and open the project in VS Code:

bash
$ touch arrays.ts

1. Create

We write an array with square brackets, and the items separated by commas. Here, three fruits. We store it in a constant, and print it.

ts
const fruits = ["apple", "banana", "cherry"];
console.log(fruits);
bash
$ node arrays.ts

2. Index

Every item has a position, called the index. And it starts at zero, not one. So the item at index zero is apple. The length property tells how many items there are.

ts
console.log(fruits[0]);
console.log(fruits.length);
bash
$ node arrays.ts

3. push

Push adds an item at the end. Pop removes the last item. Even though our fruit list is a constant, we can change what is inside the array. The constant only means that the name always points to the same array.

ts
fruits.push("mango");
console.log(fruits);
fruits.pop();
console.log(fruits.length);
bash
$ node arrays.ts

4. Past the end

What if we ask for position number five? There is no item there. Node does not crash, it just gives us undefined. This is a common source of bugs, so keep it in mind.

ts
console.log(fruits[5]);
bash
$ node arrays.ts

5. Wrong type

TypeScript knows that our list holds strings. What if we push the number forty two? Node does it without a word, and now our fruit list has a number in it.

ts
fruits.push(42);
console.log(fruits);
bash
$ node arrays.ts

6. tsc

tsc stops it: argument of type number is not assignable to parameter of type string. An array of strings only accepts strings. TypeScript protects the list.

bash
$ npx tsc --noEmit arrays.ts

7. Fix

We delete those two lines, and tsc is quiet again.

bash
$ npx tsc --noEmit arrays.ts

Recap

Today: arrays with square brackets, the index starting at zero, length, push and pop, undefined past the end, and an array of strings only accepts strings. Next episode: objects.

ยฉ 2026 All Rights Reserved By Mahmudul Alam