配列リテラル

配列リテラル([])を使うと配列を作ることができる

初期値も設定できる

const numbers = [1, 2, 3];

配列の型注釈

型名[]型名<Array> を使うと配列を定義できる

let numbers: number[];
let strings: Array<string>;

配列要素へのアクセス

添え字(インデックス)を使えばアクセスできる

const colors = ["red", "green", "blue"];
console.log(colors[0]); // red
colors[1] = "yellow";
console.log(colors); // ['red', 'yellow', 'blue'

読み取り専用配列

型注釈にreadonlyReadonlyArray<型名> で読み取り専用配列を定義することができる

const numbers: readonly number[] = [1, 2, 3];
const strings: ReadonlyArray<string> = ["hello", "world"];

numbers[0] = 4; // [エラー]
strings.push("!"); // [エラー]

配列によるループ

for (... of ...) で配列を使ったループを定義できる

const numbers = [1, 2, 3];

for (const num of numbers) {
  console.log(num);
}
/*
1
2
3
*/