CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❤️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

⬅️ Array.prototype.shift() Polyfill

The shift() method removes and returns the first array element. Modifies the array in place, shifting all remaining elements down.

The method removes the first element from an array and returns that element. -- ✅ Implementation -- How It Works 1. Handles an empty array If , it returns , just like . 2. Stores the first element Saves before modifying the array. 3. Shifts elements to the left Iterates through the array and shifts elements to the left (). 4. Reduces the array length effectively removes the last duplicate. 5. Returns the removed element Mimics behavior by returning the removed item. -- Edge Cases Handled ✔ Works on normal arrays ✔ Works on empty arrays () ✔ Modifies the original array in place ✔ Returns the first element as expected This implementation is simple, efficient, and follows exactly. -- <!-quiz-start --Q1: What does return? [ ] null [ ] An empty array [x] undefined [ ] 0 Q2: What is the time complexity of the 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 --
JavaScriptPolyfills
🎯 Array.prototype.at() Polyfill
medium
✅ Array.prototype.every() Polyfill
medium
🔲 Array.prototype.fill() Polyfill
medium
🔍 Array.prototype.filter() Polyfill
medium
🔎 Array.prototype.find() Polyfill
medium
🔢 Array.prototype.findIndex() Polyfill
medium
🔙 Array.prototype.findLast() Polyfill
medium
🔙 Array.prototype.findLastIndex() Polyfill
medium
📋 Array.prototype.flat() Polyfill
medium
🔍 Array.prototype.includes() Polyfill
medium
🔢 Array.prototype.indexOf() Polyfill
medium
✅ Array.isArray() Polyfill
medium
🗺️ Array.prototype.map() Polyfill
hard
➖ Array.prototype.pop() Polyfill
medium
➕ Array.prototype.push() Polyfill
easy
🔄 Array.prototype.reduce() Polyfill
medium
🔄 Array.prototype.reverse() Polyfill
hard
⬅️ Array.prototype.shift() Polyfill
hard
🔘 Array.prototype.some() Polyfill
medium
🔀 Array.prototype.sort() Polyfill
hard
➡️ Array.prototype.unshift() Polyfill
hard
📞 Function.prototype.apply() Polyfill
medium
🔗 Function.prototype.bind() Polyfill
hard
📞 Function.prototype.call() Polyfill
medium
18 of 24
LibraryJavaScriptPolyfills33 of 61

⬅️ Array.prototype.shift() Polyfill

jspolyfillshard

The shift() method removes the first element from an array and returns that element.


✅ Implementation

Array.prototype.customShift = function () {
    if (this.length === 0) return undefined; // Return undefined if the array is empty

    const firstElement = this[0]; // Store the first element

    // Shift all elements to the left
    for (let i = 1; i < this.length; i++) {
        this[i - 1] = this[i];
    }

    this.length -= 1; // Reduce the length of the array

    return firstElement; // Return the removed element
};

// Example usage:
const array = [1, 2, 3];
const firstElement = array.customShift();

console.log(firstElement); // Output: 1
console.log(array);        // Output: [2, 3]
console.log([].customShift()); // Output: undefined (empty array case)

How It Works

  1. Handles an empty array
    • If this.length === 0, it returns undefined, just like shift().
  2. Stores the first element
    • Saves this[0] before modifying the array.
  3. Shifts elements to the left
    • Iterates through the array and shifts elements to the left (this[i - 1] = this[i]).
  4. Reduces the array length
    • this.length -= 1; effectively removes the last duplicate.
  5. 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.


Quick Quiz

Test your understanding with 3 quick questions

Q1What does `[].customShift()` return?
Q2What is the time complexity of the `shift()` operation?
Q3How does the shift polyfill work internally?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❤️ by Tushar Khanna