Array.prototype.shift()

y0ung·2020년 11월 11일
0

⚙ API-JS

목록 보기
2/7

shift()?

배열에서 첫 번째 요소를 제거하고, 제거된 요소를 반환한다.
이 메서드는 배열의 길이를 변하게 한다.

📝 구문

arr.shift()

반환값

  • 배열에서 제거한 요소
  • 빈 배열일 경우 undefined반환

🔘 예제

예제1
let color = ['red','orange','yellow','green', 'blue];
             
console.log(`colors: ${color}`);// red,orange,yellow,green,blue

let shifted = color.shift();

console.log(color) // orange,yellow,green,blue;

console.log(shifted); // red
예제2: while문 안에서 shift()
var number = ["one", "two", "three", "four"];

while( (i = number.shift()) !== undefined ) {
    console.log(i);
}
// one, two, three, four

참고
shift() mdn

profile
어제보다는 오늘 더 나은

0개의 댓글