All lessons

Arrays

7 min · TypeScript

Hold many values under one typed name, and reach for the one you want.

TypeScript · Arrays0/5 cleared

Enemy 1 of 5

An array holds several values in order, inside [ ], separated by commas — one name, many values, exactly as in JavaScript. TypeScript lets you label what kind of values it should hold: string[] means "an array of strings," number[] means "an array of numbers."

Once an array is typed this way, TypeScript enforces it everywhere the array is used: trying to fruits.push(5) on a string[] would be flagged as an error immediately, since 5 isn't a string. This is one of the clearest everyday wins of typed arrays — a whole class of "I accidentally put the wrong kind of thing in this list" bugs simply can't happen.

You
Kernel Hellhound

Loading editor...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: [ 'apple', 'banana', 'cherry' ]