
- 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
what are the differences between unshift() and push() methods in javascript?
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.
1) push()
Array.push() method is used to add an element at the end of an array like a queue .In the following example it is shown how to add an element using push() method
Example
<html> <body> <script> var cars = ["Benz", "Lamborghini", "Tata safari"]; cars.push("Ferrari"); document.write(cars); </script> </body> </html>
Output
Benz,Lamborghini,Tata safari,Ferrari
2) unshift()
Array.unshift() method is used to add an element at the starting of an array.
Example
<html> <body> <script> var cars = ["Benz", "Lamborghini", "Tata safari"]; cars.unshift("Ferrari"); document.write(cars); </script> </body> </html>
Output
Ferrari,Benz,Lamborghini,Tata safari
- Related Articles
- Difference between push() and unshift() methods in javascript
- Push() vs unshift() in JavaScript?
- What are the differences between findElement() and findElements() methods?
- What are the differences between class methods and class members in C#?
- What are the differences between compareTo() and compare() methods in Java?\n
- What are the differences between close() and quit() methods in Selenium with python?
- What are the differences between switch_to_default_content() and switch_to.parent_frame() methods in Selenium with python?
- What are the differences between current_window_handle and window_handles methods in Selenium with python?
- What are the differences between mean.io and mean.js in javascript?
- What are the differences between clientHeight() and offsetHeight() in javascript?
- What are the differences between JavaScript and PHP cookies?
- What are the differences between Deferreds, Promises and Futures in javascript?
- What are the differences between inline JavaScript and External file?
- What are the differences between JavaScript Primitive Data Types and Objects?
- Differences between wait() and join() methods in Java

Advertisements