⬅️ Array.prototype.shift() Polyfill
Category: js / polyfills
Difficulty: hard
The shift() method removes the first element from an array and returns that element. ✅ Implementation [code example] How It Works Handles an empty array If this.length === 0, it returns undefined, just like shift(). Stores the first element Saves this[0] before modifying the array. Shifts elements to the left Iterates through the array and shifts elements to the left (this[i - 1] = this[i]). Reduces the array length this.length -= 1; effectively removes the last duplicate. Returns the removed element Mimics shift() behavior by returning the removed item. Edge Cases Handled ✔ Works on normal arrays ✔ Works on empty arrays ([].customShift() → undefined) ✔ Modifies the original array in place ✔ Returns the first element as expected This implementation is simple, efficient, and follows Array.prototype.shift exactly. <!-- quiz-start --> Q1: What does [].customShift() return? [ ] null [ ] An empty array [x] undefined [ ] 0 Q2: What is the time complexity of the shift() operation? [ ] O(1) [x] O(n) [ ] O(log n) [ ] O(n^2) Q3: How does the shift polyfill work internally? [ ] Removes the first element using delete [x] Shifts all elements left by one position and decreases length [ ] Creates a new array without the first element [ ] Uses splice() to remove the first element <!-- quiz-end -->...