JavaScript - push
조회수: 47
const fruits = ['apple', 'banana', 'orange', 'melon'];
과일 배열이 있다.
push를 이용해서 배열 끝에 추가를 할 수 있다.
fruits.push('blueberry');
console.log(fruits);
결과는 ['apple', 'banana', 'orange', 'melon', 'blueberry'].
블루베리가 추가된 것을 확인할 수 있다.
1개 말고 동시에 2개를 추가할 수도 있다.
fruits.push('peach', 'pear');
console.log(fruits);
['apple', 'banana', 'orange', 'melon', 'blueberry', 'peach', 'pear']
1개 추가하고 2가 추가해서 총 7개가 되었다.
console.log(fruits.length)
// 7
과일 배열이 있다.
push를 이용해서 배열 끝에 추가를 할 수 있다.
fruits.push('blueberry');
console.log(fruits);
결과는 ['apple', 'banana', 'orange', 'melon', 'blueberry'].
블루베리가 추가된 것을 확인할 수 있다.
1개 말고 동시에 2개를 추가할 수도 있다.
fruits.push('peach', 'pear');
console.log(fruits);
['apple', 'banana', 'orange', 'melon', 'blueberry', 'peach', 'pear']
1개 추가하고 2가 추가해서 총 7개가 되었다.
console.log(fruits.length)
// 7