WHAT'S NEW
TypeScript · Lesson 05 of 11

Working with Arrays

Store multiple values in one place! Discover how TypeScript arrays organize collections, enforce element types, and help you work with lists of information.

Typed Arrays Indexes Array Methods Loops
● ● ●   arrays.ts
const skills: string[] = [
  "HTML",
  "CSS",
  "TypeScript"
];

skills.push("JavaScript");
console.log(skills);
CONSOLE OUTPUT
> Ready to run…
01 / Understanding Arrays

Store More Than One Value

Imagine you’re tracking all the programming languages you want to learn. You could create a separate variable for every language, but there’s a better way.

An array stores multiple values together in an ordered collection. Instead of managing many separate variables, you can organize related information in one place.

arrays.ts
const languages: string[] = [
  "HTML",
  "CSS",
  "TypeScript"
];

console.log(languages);
Console Output:
[“HTML”,”CSS”,”TypeScript”]

The square brackets contain the array’s elements. The string[] annotation tells TypeScript that every element should be a string.

Remember: An array is a collection, while an element is one individual value inside that collection.
02 / Typed Arrays

Keep Your Collections Consistent

TypeScript lets you describe the type of values an array should contain. This helps prevent unexpected data from entering your collection.

typed-arrays.ts
const scores: number[] = [80, 95, 100];
const names: string[] = ["Moe", "Jordan"];
const completed: boolean[] = [true, false];

console.log(scores);
console.log(names);
console.log(completed);

Each array has a different element type: numbers, strings, or booleans.

Type mismatch example
const scores: number[] = [80, 95, 100];

// TypeScript error:
scores.push("excellent");

Because scores is a number[], adding a string is not allowed.

Good habit: Use a specific element type whenever possible. It makes your code easier to understand and helps TypeScript catch mistakes.
03 / Array Indexes

Access Individual Items

Each array element has a position called an index. TypeScript arrays use zero-based indexing, meaning the first element is at index 0.

indexes.ts
const skills: string[] = [
  "HTML",
  "CSS",
  "TypeScript"
];

console.log(skills[0]);
console.log(skills[1]);
console.log(skills[2]);
Console Output:
HTML CSS TypeScript

The first element is skills[0], not skills[1]. If you access an index outside the array, JavaScript normally returns undefined at runtime.

04 / Updating Elements

Change an Item in Your Array

You can update an existing array element by assigning a new value to its index.

update-array.ts
const skills: string[] = [
  "HTML",
  "CSS",
  "JavaScript"
];

skills[2] = "TypeScript";

console.log(skills);
Console Output:
[“HTML”,”CSS”,”TypeScript”]

Notice that we declared the array using const. That’s fine: const prevents reassigning the array variable itself, but you can still update its elements.

05 / Adding Items

Grow Your Collection with Push

The push() method adds one or more elements to the end of an array.

push.ts
const skills: string[] = ["HTML", "CSS"];

skills.push("TypeScript");
skills.push("Python");

console.log(skills);
Console Output:
[“HTML”,”CSS”,”TypeScript”,”Python”]

The push() method also returns the new array length, although you don’t have to use that returned value.

Try it: Add another programming language to the array. Remember that the value must match the array’s declared element type.
06 / Removing Items

Remove the Last Element with Pop

The pop() method removes the final element from an array and returns that element. If the array is empty, it returns undefined.

pop.ts
const skills: string[] = [
  "HTML",
  "CSS",
  "TypeScript"
];

const removed = skills.pop();

console.log(removed);
console.log(skills);
Console Output:
TypeScript [“HTML”,”CSS”]

Use push() to add to the end and pop() to remove from the end.

07 / Array Length

Find Out How Many Items You Have

Every array has a length property that tells you how many elements it contains.

length.ts
const skills: string[] = [
  "HTML",
  "CSS",
  "TypeScript"
];

console.log(skills.length);
Console Output:
3

The length is 3, even though the final index is 2. That’s because indexes start at zero, while length counts the number of elements.

08 / Arrays and Loops

Work Through Every Element

Arrays become especially powerful when combined with loops. Instead of manually accessing every index, you can repeat instructions for each element.

array-loop.ts
const skills: string[] = [
  "HTML",
  "CSS",
  "TypeScript"
];

for (let i: number = 0; i < skills.length; i++) {
  console.log(skills[i]);
}
Console Output:
HTML CSS TypeScript

The loop starts at index 0 and continues while i is less than the array’s length.

A Simpler Alternative: for…of

for-of.ts
const skills: string[] = [
  "HTML",
  "CSS",
  "TypeScript"
];

for (const skill of skills) {
  console.log("Learning " + skill);
}
Console Output:
Learning HTML Learning CSS Learning TypeScript

A for...of loop gives you each value directly, without requiring an index variable.

09 / Array Type Inference

Let TypeScript Recognize Your Collection

Just as TypeScript can infer the types of ordinary variables, it can often infer array element types.

array-inference.ts
const developers = ["Moe", "Jordan"];
const levels = [1, 2, 3];

developers.push("Taylor");
levels.push(4);

// Type error:
// levels.push("five");

TypeScript infers that developers contains strings and levels contains numbers.

Good habit: Explicit annotations such as string[] are helpful for clarity, especially when declaring an empty array whose intended element type isn’t otherwise obvious.
10 / Putting It Together

Build a Learning List

Let’s create a list of skills Moe is learning, add a new skill, and display everything using a loop.

learning-list.ts
const developer: string = "Moe";
const skills: string[] = ["HTML", "CSS"];

skills.push("TypeScript");

console.log(developer + "'s learning list:");

for (let i: number = 0; i < skills.length; i++) {
  console.log((i + 1) + ". " + skills[i]);
}

console.log("Total skills: " + skills.length);
Console Output:
Moe’s learning list: 1. HTML 2. CSS 3. TypeScript Total skills: 3

This small program combines typed arrays, array methods, indexes, loops, and string output into one practical example.

🚀 mC0de Challenge

Create Your Developer Learning List

Build a personalized collection of programming skills and use TypeScript to display your progress.

Your Mission

  1. Change the developer name if you’d like.
  2. Add a new programming skill using push().
  3. Change one existing skill using its array index.
  4. Use a loop to print every skill in the list.
  5. Display the total number of skills using length.
  6. Run your code and check the output.
Extra challenge: Try adding a number to the string[] using push(42). Read the TypeScript error, then replace it with a valid string.
💻 Interactive TypeScript Sandbox

Build Your Learning List!

Edit the array, add skills, and run the program. The sandbox checks TypeScript types before executing the compiled JavaScript.

Console Output
Lesson 05 Recap

What You’ve Learned

You’re Organizing Data Like a Developer!

You can now:

  • Create typed arrays using string[], number[], and boolean[].
  • Access and update array elements using indexes.
  • Add elements using push().
  • Remove the last element using pop().
  • Count array elements using length.
  • Loop through arrays using for and for...of.
  • Recognize type errors when inserting incompatible values.

Next up: Objects — learn how to organize related information using named properties and TypeScript types!

Your Learning Journey

0 / 11 Completed

Keep going! Every completed lesson brings you closer to building reliable applications with TypeScript.

🎉

Another Skill Unlocked!

You’ve learned how to create typed arrays, manage their elements, add and remove items, and loop through collections. You’re now ready to organize larger amounts of data in TypeScript!

PIX0
</> {} JS 01
0101 </> CSS JS HTML PIX0 const {} 1010 async mC0de function return 0101 <div> 1010 PIX0 CSS JS HTML const {} 0101 mC0de
PIX0 // WORLD OF CODE
PIX0 IS PREPARING SOMETHING AWESOME FOR YOU 0%