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.
- All episodes are in the TypeScript from Zero playlist.
- Versions used: Node.js 26.13.0, npm 11.20.0, TypeScript 7.0.2.
Setup
We start in the terminal and open the project in VS Code:
$ touch arrays.ts1. 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.
const fruits = ["apple", "banana", "cherry"];
console.log(fruits);$ node arrays.ts2. 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.
console.log(fruits[0]);
console.log(fruits.length);$ node arrays.ts3. 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.
fruits.push("mango");
console.log(fruits);
fruits.pop();
console.log(fruits.length);$ node arrays.ts4. 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.
console.log(fruits[5]);$ node arrays.ts5. 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.
fruits.push(42);
console.log(fruits);$ node arrays.ts6. 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.
$ npx tsc --noEmit arrays.ts7. Fix
We delete those two lines, and tsc is quiet again.
$ npx tsc --noEmit arrays.tsRecap
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.