This post was most recently updated on July 31st, 2024
Definition:
shift(): this array method is used to remove first element of an array and returns the new array.
pop(): this array method is used to remove last element of an array and returns the new array.
See the below code example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//JavaScript: array.shift() var myArray1 = ['Steve', 'James', 'Ombala', 'Marksen', 'Jackson']; var removedFirst = myArray1.shift(); //expected output after removed first array element is: console.log(myArray1); // ['James', 'Ombala', 'Marksen', 'Jackson'] document.write(myArray1); // James, Ombala, Marksen, Jackson //expected removed first element is: console.log(removedFirst); // 'Steve' //JavaScript: array.pop() var myArray2 = ['Steve', 'James', 'Ombala', 'Marksen', 'Jackson']; var removedLast = myArray2.pop(); //expected output after removed last array element is: console.log(myArray2); // ['Steve', 'James', 'Ombala', 'Marksen'] document.write(myArray2); // Steve, James, Ombala, Marksen //expected removed last element is: console.log(removedLast); // 'Jackson' |