create
Create a new array to store one or more values.
Create an array
Empty array
You can create arrays with no items. If you do that, you’ll need tell what type the array will be. This is an empty array:
let empty: string[] = []
let empty: string[] = []
One item array
You can create and initialize an array with an item. Arrays created with at least one item automatically have the type of the item. This is called a type inference.
let oneItem = ["item"]
Multiple item array
Create arrays with more than one item if you want.
let twoItems = [1, 2]
Examples
Make an empty array of numbers. Then, add two numbers to the array.
let scores: number[] = [];
scores.push(98);
scores.push(75);
let lastScore = scores.pop();
Make an array with two words, then swap them in the array.
let words = ["Hello", "there"]
let swap = words[0];
words[0] = words[1];
words[1] = swap;