

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between push() and unshift() methods in javascript
The unshift method adds the element at the zeroeth index and shifts the values at consecutive indexes up, then returns the length of the array.
The push() method adds the element at end to 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.push("pinapple")) console.log(fruits2.unshift("pinapple")) console.log(fruits) console.log(fruits2)
Output
5 5 [ 'apple', 'mango', 'orange', 'kiwi', 'pinapple' ] [ 'pinapple', 'apple', 'mango', 'orange', 'kiwi' ]
Note that both the original arrays were changed here.
Unshift is slower than push because it also needs to unshift all the elements to the left once the first element is added.
- Related Questions & Answers
- what are the differences between unshift() and push() methods in javascript?
- Push() vs unshift() in JavaScript?
- Difference between shift() and pop() 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
- Difference between Constructors and Methods in Java
- What is the difference between Math.ceil() and Math.round() methods in JavaScript?
- What is the difference between instanceof() and Array.isArray() methods in JavaScript?
- Difference between find() and findOne() methods in MongoDB?
- Difference between BeforeClass and BeforeTest methods in TestNG
- Difference between the list() and listFiles() methods in Java
- Difference between title() and wm_title() methods in Tkinter class
- What is the difference between non-static methods and abstract methods in Java?
- What is the difference between scipy.cluster.vq.kmeans() and scipy.cluster.vq.kmeans2() methods?
Advertisements