Definition:
shift(): this method is use to remove first element of an array and returns new array.
unshift(): this array method is used to add one or more arrays in existing array list and returns new array.
usages of shift():
1 2 3 4 5 6 7 8 9 |
//JavaScript: array.shift() var myArray1 = ['Steve', 'James', 'Ombala', 'Marksen', 'Jackson']; var removedFirst = myArray1.shift(); //expected output after removed first array element is: document.write(myArray1); // James, Ombala, Marksen, Jackson console.log(removedFirst); // Steve |
usages of unshift():
1 2 3 4 5 6 7 8 9 10 |
//JavaScript: array.unshift() var newArray = ['Steve', 'James', 'Ombala', 'Marksen', 'Jackson']; var addNew = newArray.unshift('Michel','Lorell'); //Check the output after add two elements: document.write(newArray); // Michel, Lorell, Steve, James, Ombala, Marksen, Jackson; console.log(newArray); // ['Michel', 'Lorell', 'Steve', 'James', 'Ombala', 'Marksen', 'Jackson']; |