The unshift() method adds one or more elements to the beginning of an array and returns the new length.
✅ Implementation
Array.prototype.customUnshift = function (...elements) { // Move existing elements to the right to make space for new elements for (let i = this.length - 1; i >= 0; i--) { this[i + elements.length] = this[i]; } // Insert new elements at the beginning for (let i = 0; i < elements.length; i++) { this[i] = elements[i]; } return this.length; // Return new array length }; // Example usage: const array = [1, 2, 3]; array.customUnshift(4, 5); console.log(array); // Output: [4, 5, 1, 2, 3] console.log(array.customUnshift(0)); // Output: 6 (new length of array) console.log(array); // Output: [0, 4, 5, 1, 2, 3]
How It Works
- Shifts existing elements to the right
- Loops backward to prevent overwriting elements.
- Moves each element to a new position based on the number of elements being added.
- Inserts new elements at the beginning
- Loops forward to place new elements in their correct positions.
- Returns the new array length
- Mimics
unshift()behavior by returning the updated length.
- Mimics
Edge Cases Handled
✔ Works with multiple elements: customUnshift(4, 5, 6)
✔ Works with empty arrays: [].customUnshift(1, 2, 3)
✔ Works with no arguments: array.customUnshift() (returns unchanged length)
✔ Preserves element order correctly
This implementation is efficient and follows the exact behavior of Array.prototype.unshift.
Quick Quiz
Test your understanding with 3 quick questions
Q1What does `unshift()` return?
Q2What is the time complexity of `unshift()`?
Q3Where does `unshift()` add the new elements?