DEV Community

Supaluck Singjan
Supaluck Singjan

Posted on

JavaScript Array Unshift, Push , Shift, Pop

Unshift, Push , Shift, Pop

Unshift is adding the first one of the array.
Push is adding the last one of the array.
Shift is deleting the first one of the array.
Pop is deleting the last one of the array.

Example

let customers = ['Eelyn', 'Ken', 'Mike']; 
Enter fullscreen mode Exit fullscreen mode
//Unshift 
customers.unshift('Winnie');
console.log(customers)
Enter fullscreen mode Exit fullscreen mode

Result :
['Winnie', 'Eelyn', 'Ken', 'Mike']


//Push
customers.push('Melissa');
console.log(customers)
Enter fullscreen mode Exit fullscreen mode

Result :
['Winnie', 'Eelyn', 'Ken', 'Mike', 'Melissa']


//Shift
customers.shift();
console.log(customers)
Enter fullscreen mode Exit fullscreen mode

Result :
['Eelyn', 'Ken', 'Mike', 'Melissa']


//Pop
customers.pop();
console.log(customers)
Enter fullscreen mode Exit fullscreen mode

Result :
['Eelyn', 'Ken', 'Mike']


Top comments (0)