[JS] 배열의 앞뒤에 위치한 요소를 삭제하는 메서드: pop(), shift()

cabbage·2023년 1월 16일

JS

목록 보기
13/43
post-thumbnail

배열에서 데이터를 삭제하기 위해 pop(), shift() 메서드를 사용할 수 있다.

Array.prototype.pop()

pop() 메서드는 배열에서 마지막 요소를 제거하고 제거한 요소를 반환한다.
pop() 메서드를 호출한 배열의 길이를 변경한다.

const arr = [1, 2, 3, 4, 5];

console.log(arr.pop());  // 5
console.log(arr);  // [1, 2, 3, 4]

arr.pop();
console.log(arr);  // [1, 2, 3]

Array.prototype.shift()

shift() 메서드는 배열에서 첫번째 요소를 제거하고 제거한 요소를 반환한다.
shift() 메서드를 호출한 배열의 길이를 변경한다.

const arr = [1, 2, 3, 4, 5];

console.log(arr.shift());  // 1
console.log(arr);  // [2, 3, 4, 5]

참고

profile
캐비지 개발 블로그입니다. :)

0개의 댓글