配列リテラル([])を使うと配列を作ることができる
初期値も設定できる
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'
型注釈にreadonlyやReadonlyArray<型名> で読み取り専用配列を定義することができる
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
*/