Menu
Coddy logo textTech

Typed Arrays

Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 13 of 73.

Typed arrays are one of TypeScript's most powerful features for organizing and managing multiple values while maintaining type safety.

In TypeScript, you can specify that an array should only contain elements of a particular type using the Type[] syntax. This ensures that every element in the array matches the expected type, preventing common errors that occur when mixing different data types.

let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Alice", "Bob", "Charlie"];
let flags: boolean[] = [true, false, true];

When you declare a typed array, TypeScript enforces that only values of the specified type can be added to it. This means you can't accidentally mix strings with numbers or other incompatible types, which helps catch errors during development rather than at runtime.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a typed array named scores that can only hold numbers and initialize it with the values 85, 92, and 78.

Then create another typed array named studentNames that can only hold strings and initialize it with the values "Alice", "Bob", and "Charlie".

Finally, create a typed array named passedExams that can only hold boolean values and initialize it with true, false, and true.

Print all three arrays to the console on separate lines in the order they were created.

Cheat sheet

Typed arrays in TypeScript ensure that arrays only contain elements of a specific type using the Type[] syntax:

let numbers: number[] = [1, 2, 3, 4, 5];
let names: string[] = ["Alice", "Bob", "Charlie"];
let flags: boolean[] = [true, false, true];

TypeScript enforces type safety by preventing you from mixing incompatible types in the same array, catching errors during development.

Try it yourself

// TODO: Write your code here
// Create the three typed arrays as described in the challenge

// Print all three arrays to the console
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Introduction To TypeScript