An Array is a list of other items that have a basic (primitive) type.

An array is a list of items that are numbers, booleans, or strings. Arrays have a length which is the number of items they contain. You get and change the values of items at different places in an array. You find items in an array by knowing their positions.

Arrays are flexible, they can grow and shrink in size. You can add and remove items at any place in the array.

Create an array

An array is created by making a list of items.

let scores = [9, 8, 3, 5, 6, 8];

Here the array automatically becomes an array of numbers because it was created with items that are numbers.

You can use different types, like string.

let directions = ["East", "North", "South", "West"];

If you want to create an array with no items in it yet (empty array), you have to tell what type the array should be. This is because there are no items in it that automatically decide the type.

let scores: number[] = [];

Items in an array

When an item is added to an array it becomes an element of the array. Array elements are found using an index. This is the position in the array of the element, or the element’s order in the array. The positions in an array begin with the index of 0. The first place in the array is 0, the second place is 1, and so on.

let scores = [9, 8, 3, 5, 6, 8];
let firstScore= scores[0];
let secondScore = scores[1];

Length of arrays

Arrays always have a length. The last element of an array has an index that is length of array - 1. Indexes begin with 0 so the last element’s index is one less than the array length.

let directions = ["East", "North", "South", "West"];
let count = directions.length;


Arrays are a very useful way to collect and organize information. There are more advanced operations and functions you can use on arrays.

Here’s an example using insertAt to insert a number into the middle of an array.

let scores = [8, 5, 9, 3, 2, 4];
scores.insertAt(3, 1);

See also