javascript
Javascript 배열
wefree
2023. 12. 14. 20:14
배열 임의 접근
let colors = ['red', 'orange'];
console.log(colors[1]); // orange
console.log(colors[5]); // undefined
colors[1] = 'green'; // 값 변경
colors[5] = 'blue';
console.log(colors); // [ 'red', 'green', <3 empty items>, 'blue' ]
위의 colors[5] = 'blue'; 가 정상적인 문법으로 동작한다. 중간을 비어 있는 값으로 채운다 !!!
기본 메서드
let colors = ['red', 'orange'];
colors.push('yellow'); // [ 'red', 'orange', 'yellow' ]
let x = colors.pop(); // [ 'red', 'orange' ], x='yellow'
colors = ['red', 'orange'];
colors.unshift('blue'); // [ 'blue', 'red', 'orange' ]
let y = colors.shift(); // [ 'red', 'orange' ], y='blue'