How to use push() & pop() Methods in JavaScript Arrays? Last Updated : 20 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Arrays are dynamic data structures in JavaScript that have the ability to hold several values of various types. The push() and pop() methods are used for adding and removing elements from an array's last index.1. Using push() Method in JavaScriptThe push() method is used to add or push the new value to the given array. it added a new value to the end of array, using this we increases the length of the array. The pushed element will always have the last index number. Syntaxarr.push(YOUR_VALUE); JavaScript let arr = [1, 2]; arr.push("3"); console.log(arr); Output[ 1, 2, '3' ] 2. Using pop() Method in JavaScriptThis pop() method is used to remove the last element from the given array. It will always remove the last element of the given array and decrease the length of the array.Syntax arr.pop(); JavaScript let arr = [1, 2, 3]; let lastNum = arr.pop(); console.log(arr); console.log(lastNum); Output[ 1, 2 ] 3 Conditional Usage of pop() and push() MethodsThis approach entails using pop() and push() inside of conditional statements or loops. This makes it possible to manipulate the array dynamically, adding and removing elements in response to specific criteria. This is helpful in situations where user input, API responses, or other runtime circumstances determine the content of the array. JavaScript let num = [1, 2, 3, 4, 5]; if (num.length > 4) { num.pop(); } console.log(num); let condition = true; if (condition) { num.push(6); } console.log(num); Output[ 1, 2, 3, 4 ] [ 1, 2, 3, 4, 6 ] Comment More infoAdvertise with us Next Article How to use push() & pop() Methods in JavaScript Arrays? H heysaiyad Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads JavaScript Array push() Method The push() method in JavaScript adds one or more elements to the end of an array and returns the new length of the array. It modifies the original array, increasing its length by the number of elements added.Syntaxarr.push(element0, element1, ⦠, elementN);ParametersThis method contains as many numb 3 min read Shift() vs pop() Method in JavaScript JavaScript shift() and pop() methods are used to remove an element from an array. But there is a little difference between them. The shift() method removes the first element and whereas the pop() method removes the last element from an array. Javascript Array shift() method: The JavaScript Array shi 2 min read How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one 2 min read JavaScript Array pop() Method The pop() method removes (pops) the last element of an array. The pop() method changes the original array. It reduces the array's length by one and returns the removed element. Syntax:arr.pop();This method does not accept any parameter.Return value:This method returns the removed element array. If t 2 min read Alternatives of push() method in JavaScript The task is to perform a push operation without using the push() method with the help of JavaScript. There are two approaches that are discussed below. Approach 1: Use the length property to insert the element at the end of the array. Example: This example implements the above approach. html <bod 2 min read Like