pop(): We use pop JavaScript to remove the last element in an array. Moreover, this function returns the removed element. At the same, it will reduce the length of the array by one. This function is the opposite of the JavaScript array push function.
js. OneSignal, Apple Push Notification Service, PushCrew, Azure Notification Hubs, and Pushwoosh are the most popular alternatives and competitors to Push. js.
Both the methods are used to add elements to the array. But the only difference is unshift() method adds the element at the start of the array whereas push() adds the element at the end of the array.
push()
adds at end; pop()
deletes from end.
unshift()
adds to front; shift()
deletes from front.
splice()
can do whatever it wants, wherever it wants.
Well, you've kind of asked two questions. The opposite of push()
(as the question is titled) is pop()
.
var exampleArray = ['myName'];
exampleArray.push('hi');
console.log(exampleArray);
exampleArray.pop();
console.log(exampleArray);
pop()
will remove the last element from exampleArray
and return that element ("hi") but it will not delete the string "myName" from the array because "myName" is not the last element.
What you need is shift()
or splice()
:
var exampleArray = ['myName'];
exampleArray.push('hi');
console.log(exampleArray);
exampleArray.shift();
console.log(exampleArray);
var exampleArray = ['myName'];
exampleArray.push('hi');
console.log(exampleArray);
exampleArray.splice(0, 1);
console.log(exampleArray);
For more array methods, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Mutator_methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With