Lesson not found
Lesson Progress: 0%
Arrays let you store multiple values of the same type in a single ordered list. In TypeScript, you declare the type of elements the array holds by writing the type followed by square brackets, like string[] for an array of strings or number[] for an array of numbers. Each value in an array is stored at a numbered position called an index, starting at 0. You can access an item by writing the array name followed by the index in square brackets, like colors[0] to get the first element. Arrays are useful whenever you have a list of related items, such as a list of names, scores, or colors. TypeScript checks that you only add values of the correct type to the array, preventing mistakes like accidentally putting a number into a string array. You can also change values inside an array after it is created, add new items to the end, or remove the last item. With arrays, you can organize and work with collections of data in a clean and predictable way.
let colors: string[] = ["red", "green", "blue"];
console.log(colors[0]);
console.log(colors[1]);
console.log(colors[2]);Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
You create an array by listing values inside square brackets and giving it a type.
string[] means the array holds only string values."red", "green", and "blue" are stored at indexes 0, 1, and 2.colors[0] gets the first element, which is "red".undefined.string[] array.let numbers: number[] = [10, 20, 30, 40];
console.log(numbers.length);
console.log(numbers[0]);
console.log(numbers[3]);Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
Every array has a length property that tells you how many items it contains.
numbers.length returns 4 because the array has four elements.length - 1, which is 3 in this case.numbers[0] gives the first element, and numbers[3] gives the last..length in a loop condition to process every element without guessing how many there are.number[] array can hold any numbers, including negatives or decimals.let fruits: string[] = ["apple", "banana", "cherry"];
fruits[1] = "mango";
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
You can change an element in an array by assigning a new value to its index.
fruits[1] = "mango" replaces whatever was at index 1 with the new value "mango"."banana", but after the change it holds "mango".string[].let scores: number[] = [90, 80];
scores.push(70);
console.log(scores.length);
console.log(scores[2]);Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
The push method adds a new element to the end of an array.
scores.push(70) adds the number 70 as a new last element.2.number[].push is a simple way to grow an array dynamically as you collect more data.let items: number[] = [1, 2, 3];
let removed: number = items.pop();
console.log(removed);
console.log(items.length);Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
The pop method removes the last element from an array and returns it.
items.pop() removes 3 (the last element) and stores it in removed.removed is typed as number because pop returns the element type.pop, only [1, 2] remain in the array.pop is useful when you want to process items in last-in-first-out order, like undoing an action.let names: string[] = ["Alice", "Bob", "Charlie"];
for (let firstName of names) {
console.log(firstName);
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
A for...of loop lets you visit every element in an array one at a time.
for (let firstName of names) goes through each element without needing an index."Alice", then "Bob", then "Charlie".name automatically gets the type string because names is string[].for...of is ideal when you want to read or process each element without changing the array.name as a number inside the loop.Test Incomplete