The difference between Push, Pop, Shift and Unshift in Javascript Array

Array is a very important concept in JavaScript and in programming as well. It is used to store related data in a single block. You can create it with the let, var or the const keyword.

//example
let arry = [“John”, “Tobi”, “Musa”];

It has so…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ewenike Chukwuemeka Emmanuel👨‍💻

Array is a very important concept in JavaScript and in programming as well. It is used to store related data in a single block. You can create it with the let, var or the const keyword.

//example
let arry = ["John", "Tobi", "Musa"];

It has some amazing methods, but we'll only explore 4.

The push() method is used to add an element at the end of the array. It also returns the added element.

arry.push("Ifeanyi").
console.log(arry);
// the result will be
["John", "Tobi", "Musa", "Ifeanyi"];

The pop() method. It removes the last element in an array, it also returns the element.
arry.pop();
console.log(arry);
//the result will be
["john", "Tobi", "Musa"];

The shift() method. This method removes the first element in an array. It behaves like the pop() method but in an opposite direction.
arry.shift();
//the result will be
["Tobi", "Musa"];

The unshift() method. This method adds a new element at the beginning of an array. It also behaves like the push() method but in an opposite direction.
arry.unshift("Ayo");
//the result will be
["Ayo", "Tobi", "Musa];


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Ewenike Chukwuemeka Emmanuel👨‍💻


Print Share Comment Cite Upload Translate Updates
APA

Ewenike Chukwuemeka Emmanuel👨‍💻 | Sciencx (2023-02-11T11:25:35+00:00) The difference between Push, Pop, Shift and Unshift in Javascript Array. Retrieved from https://www.scien.cx/2023/02/11/the-difference-between-push-pop-shift-and-unshift-in-javascript-array/

MLA
" » The difference between Push, Pop, Shift and Unshift in Javascript Array." Ewenike Chukwuemeka Emmanuel👨‍💻 | Sciencx - Saturday February 11, 2023, https://www.scien.cx/2023/02/11/the-difference-between-push-pop-shift-and-unshift-in-javascript-array/
HARVARD
Ewenike Chukwuemeka Emmanuel👨‍💻 | Sciencx Saturday February 11, 2023 » The difference between Push, Pop, Shift and Unshift in Javascript Array., viewed ,<https://www.scien.cx/2023/02/11/the-difference-between-push-pop-shift-and-unshift-in-javascript-array/>
VANCOUVER
Ewenike Chukwuemeka Emmanuel👨‍💻 | Sciencx - » The difference between Push, Pop, Shift and Unshift in Javascript Array. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/11/the-difference-between-push-pop-shift-and-unshift-in-javascript-array/
CHICAGO
" » The difference between Push, Pop, Shift and Unshift in Javascript Array." Ewenike Chukwuemeka Emmanuel👨‍💻 | Sciencx - Accessed . https://www.scien.cx/2023/02/11/the-difference-between-push-pop-shift-and-unshift-in-javascript-array/
IEEE
" » The difference between Push, Pop, Shift and Unshift in Javascript Array." Ewenike Chukwuemeka Emmanuel👨‍💻 | Sciencx [Online]. Available: https://www.scien.cx/2023/02/11/the-difference-between-push-pop-shift-and-unshift-in-javascript-array/. [Accessed: ]
rf:citation
» The difference between Push, Pop, Shift and Unshift in Javascript Array | Ewenike Chukwuemeka Emmanuel👨‍💻 | Sciencx | https://www.scien.cx/2023/02/11/the-difference-between-push-pop-shift-and-unshift-in-javascript-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.