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.unshift() Polyfill

The unshift() method adds elements to the beginning of an array and returns the new length. Modifies the array in place efficiently.

The method adds one or more elements to the beginning of an array and returns the new length. -- ✅ Implementation -- How It Works 1. 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. 2. Inserts new elements at the beginning Loops forward to place new elements in their correct positions. 3. Returns the new array length Mimics behavior by returning the updated length. -- Edge Cases Handled ✔ Works with multiple elements: ✔ Works with empty arrays: ✔ Works with no arguments: (returns unchanged length) ✔ Preserves element order correctly This implementation is efficient and follows the exact behavior of . -- <!-quiz-start --Q1: What does return? [ ] The added elements [ ] The original array [x] The new length of the array [ ] undefined Q2: What is the time complexity of ? [ ] O(1) [x] O(n) [ ] O(log n) [ ] O(n^2) Q3: Where does add the new elements? [x] At the beginning of the array [ ] At the end of the array [ ] At a random position [ ] Sorted into the correct position <!-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
21 of 24
LibraryJavaScriptPolyfills36 of 61

➡️ Array.prototype.unshift() Polyfill

jspolyfillshard

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

  1. 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.
  2. Inserts new elements at the beginning
    • Loops forward to place new elements in their correct positions.
  3. Returns the new array length
    • Mimics unshift() behavior by returning the updated length.

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?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❤️ by Tushar Khanna