Shift and Unshift in javascript

In this tutorial, we will explore how to manipulate arrays using two functions, shift and unshift, in JavaScript. These functions allow us to add or remove elements from the beginning of an array.

Arrays are a crucial component in programming tasks across different languages, including JavaScript. They allow for the storage of similar data types such as strings, integers, arrays, and even functions.

As programmers, we often encounter situations where we need to add or remove elements from an array. This is where the javascript shift and unshift methods come in handy. In this post, we will delve into these two functions shift and unshift JavaScript and provide examples to help you gain a deeper understanding of their usage.

So Let’s see how to use shift and unshift in javascript. For difference between shift and unshift in javascript, let us study shift and unshift individually.

shift and unshift in javascript

shift() method

JavaScript’s Array.shift() method is a handy tool for manipulating arrays. When you use it, the first element in the array is removed and returned as a result. This means that you can easily get rid of the first item in an array with just one line of code.

One thing to keep in mind is that the shift() method alters the length of the array. It’s similar to the pop() method, but with one key difference: pop() removes the last element in the array, whereas shift() removes the first.

So if you want to remove the first element of an array, use the shift() method. It’s a quick and efficient way to modify the contents of an array, and it can help you streamline your code and make it more efficient. Just be aware that the method will change the length of the array, so you may need to adjust your code accordingly.

So In-short this function takes the first element of an array and removes it, then shifts the remaining elements one index to the left. This means that the second element becomes the first, the third becomes the second, and so on.

Syntax :

array.shift();

Here, array is the name of an array. The shift() method does not accept any arguments. It returns undefined if the array is empty. i.e. if the length property is 0, the shift() returns undefined as a return value.

Example :

const myArray = ["apple", "banana", "orange", "pear"];
const removedItem = myArray.shift();
console.log(myArray);
console.log(removedItem);

Output :

["banana", "orange", "pear"]
"apple"

In this code, we start with an array of fruits. When we call shift(), it removes the first element, “apple”, and shifts the remaining elements to the left. The new array, myArray, contains only “banana”, “orange”, and “pear”. We also store the removed item in the variable removedItem, which we can log to the console to see that it is “apple”.

unshift() method

The JavaScript Array unshift() method is a useful tool for adding or inserting elements at the beginning of an array. This method differs from the push() method, which adds elements to the end of the array.

When you use the unshift() method, you can add one or more elements to the beginning of an array. This method returns the new length of the array after the insertion of the new elements.

Compared to the push() method, which adds elements to the end of the array, the unshift() method allows you to add elements to the beginning of the calling array.

By using the unshift() method, you can easily modify the contents of an array, making it a valuable tool in JavaScript programming.

In-short unshift() function is the opposite of shift() – it adds an element to the beginning of an array and shifts the remaining elements one index to the right.

Syntax :

array.unshift(element1, element2, .....)

Here, array is the name of an array. The element1, element2, . . . ., elementN are the new elements to be added at the beginning of the calling array.

The unshift() method returns the modified length of array after adding of new elements.

Example :

const myArray = ["banana", "orange", "pear"];
myArray.unshift("apple");
console.log(myArray); 

Output :

["apple", "banana", "orange", "pear"]

Conclusion :

It’s worth noting that shift() and unshift() only work on the first element of an array, unlike push() and pop() which work on the last element. This means that shift() and unshift() are useful when you need to maintain the order of the elements in the array.

In conclusion, shift() and unshift() are powerful tools for adding or removing elements from the beginning of an array in JavaScript. By understanding how they work, you can manipulate arrays more effectively in your programs.

Follow for More Info – https://instagram.com/developerhelps?igshid=MzRlODBiNWFlZA==