Unshift method of array in javascript
JavaScript unshift() Method – Explanation and Description
The unshift() method in JavaScript is used to add one or more elements to the beginning of an array. It is a mutating method, meaning that it directly modifies the original array by inserting new elements at the start. After calling unshift(), the length of the array increases by the number of elements added.
Key Characteristics of unshift() Method:
Adds elements to the front of the array: Unlike the push() method, which adds elements to the end of an array, unshift() places them at the beginning.
Modifies the original array: unshift() is a mutating method, meaning the original array is changed directly.
Returns the new length of the array: After the elements are added, unshift() returns the updated length of the array, not the new array itself.
Can add multiple elements at once: You can pass one or more values as arguments to unshift() to add multiple elements to the beginning of the array in a single operation.
Syntax:
array.unshift(element1, element2, ..., elementN);
element1, element2, ..., elementN: These are the elements you want to add to the front of the array. You can pass one or more values separated by commas.
Return Value:
Returns the new length of the array after the elements have been added.
Key Takeaways:
• The unshift() method adds one or more elements to the beginning of the array.
• It modifies the original array and returns the new length of the array.
• You can add multiple elements at once by passing them as separate arguments.