728x90

✨ 배열(array)의 가장 앞에 값을 넣어주고, 새로운 array의 길이를 반환하는 helper method
(Queue와 같이 First In이 필요한 상황에서 사용할 수 있다)

💻Example Code

const arr = [ 3, 4 ];
arr.unshift(1, 2);

console.log( arr );

실행 결과(기존 3, 4 앞에 1, 2가 삽입된 것을 볼 수 있다)

😋 큐(Queue) 자료구조(Data Structure)처럼 항상 값이 가장 앞에 들어가야하는 경우 사용할 수 있다.
큐 자료구조는 JavaScript - Data Structures에서 확인할 수 있다.

👉 자세한 내용은 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift

 

Array.prototype.unshift()

The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.

developer.mozilla.org

 

'JavaScript > Built-in Method etc.' 카테고리의 다른 글

Array.prototype.push()  (0) 2020.01.06
Array.prototype.pop()  (0) 2020.01.04
String.fromCharCode()  (0) 2020.01.01
Array.prototype.indexOf()  (0) 2020.01.01
String.prototype.match()  (0) 2019.12.31

+ Recent posts