첫 번째 자바스크립트 데이터구조(데이터의 집합)
배열은 값의 집합이다.
배열은 값의 순서 있는 집합이다.
배열 만들기
// To make an empty array
let students = [ ];
// An array of strings
let colors = [‘red’, ‘orange’, ‘yellow’];
// An array of numbers
let lottoNums = [19, 22, 56, 12, 51];
// A mixed array
let stuff = [true, 68, ‘cat’, null];
배열에서 .length의 값은 항상 인덱스의 끝 값보다 1이 크다.
—> colors.length = 3출력 / colors[2] = ‘yellow’
배열 데이터 접근
colors[0] ; //‘red’
lottoNums[4]; //‘51’
배열의 인덱스 값을 통해 집합체 안에 들어있는 순서의 숫자로 데이터를 접근할 수 있다.
배열 수정
let colors = [‘red’, ‘orange’, ‘yellow’];
colors[0] = ‘apple’;
colors[1] = ‘pineapple’;
colors[2] = ‘lemon’;
colors[5] = ‘melon’;
colors = [‘apple’, ‘pineapple’, ‘lemon’, empty*2, ‘melon’];
colors.length; // 6
colors[4]; // undefined
배열에 값이 다 차 있지는 않지만 6개의 칸이 만들어진 것.