
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Difference between shift() and pop() methods in Javascript
The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value. If the length property is 0, undefined is returned.
The pop() method removes the last element from an array and returns that element. This method changes the length of the array.
Example
let fruits = ['apple', 'mango', 'orange', 'kiwi']; let fruits2 = ['apple', 'mango', 'orange', 'kiwi']; console.log(fruits.pop()) console.log(fruits2.shift()) console.log(fruits) console.log(fruits2)
Output
kiwi apple [ 'apple', 'mango', 'orange' ] [ 'mango', 'orange', 'kiwi' ]
Note that both the original arrays were changed here.
Shift is slower than pop because it also needs to shift all the elements to the left once the first element is removed.
- Related Articles
- Shift() vs pop() methods in JavaScript?
- Difference between OOP and POP
- Difference between push() and unshift() methods in javascript
- Difference between test () and exec () methods in Javascript
- What is the difference between functions and methods in JavaScript?
- Low level difference between Slice and Splice methods in Javascript
- What is the difference between Math.ceil() and Math.round() methods in JavaScript?
- Difference between Constructors and Methods in Java
- Difference between find() and findOne() methods in MongoDB?
- Difference between BeforeClass and BeforeTest methods in TestNG
- Explain JavaScript Bitwise NOT, Left shift and Right shift?
- What is the difference between del, remove and pop on lists in python ?
- What is the difference between non-static methods and abstract methods in Java?
- Difference between the list() and listFiles() methods in Java
- Difference between title() and wm_title() methods in Tkinter class

Advertisements