데이터 묶음
let array =[1,2,3,4,5,'gd', 'gjgjgj'];
let array =[1,2,3,4,5,'gd', 'gjgjgj'];
console.log(array[0])//1
console.log(array[3])//3
console.log(array[5])// 'gd'
console.log(array[7])// 'undefined' 배열의 인덱스를 넘어갔기 때문에
인덱스값으로 접근하여 재할당할 수 있다.
array[0]으로 접근해서 3을 재할당해주면
array[0] = 3;
console.log(array); // [ 3, 2, 3, 4, 5, 'gd', 'gjgjgj' ]
우리가 예상한대로 배열의 [0]번째 인덱스의 값이 3으로 바뀌었다.
만약 array[10]에 값을 할당하면 어떻게 될까
array[10] = 10;
console.log(array);// [ 3, 2, 3, 4, 5, 'gd', 'gjgjgj', <3 empty items>, 10 ]
10번인덱스까지 배열은 비게 되고 그 비어있는 데이터에 접근하면 undefined가 리턴된
console.log(array[9]) // undefined
여기에서 우리는 문자열도 배열과 똑같이 접근할 수 있었는데 문자열은 재할당이 되지 않는다.
문자열은 imutal이기 때문에
let firstName='lee';
firstName[0] = 'c';
console.log(firstName) // 'cee'가 아니라 변수의 기존값인 'lee'가 리턴됨