With the unshift method, we are going to add new elements to the beginning of the array. This is pretty much straightforward, and the method is easy to use. We just need to give the elements that are to be added at the beginning of the array, and those elements are added to the beginning of the array. The return value for this method is the new length of the array.
Let’s have a look at the below example, which tries to demonstrate the use of the unshift method.
As you can see, we have an array here, and we are trying to add a new element using the unshift method, and then printing the array again. If we have a look at the output, we can simply find that the new element gets added to the array. Let’s have a look at the output –
As you can see, we could add the data ‘Something’, to the beginning of the array. Remember that the unshift method is going to return the new length of the array. You can try using this method for more examples, for more practice, and we can use this method as and when required.