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

The push() method adds elements to the end of an array and returns the new length. Modifies the array in place for efficient appending.

The method adds one or more elements to the end of an array and returns the new length. -- ✅ Implementation -- How It Works 1. Uses the rest parameter () Allows multiple arguments to be passed and handled dynamically. 2. Iterates over new elements Adds each at , ensuring insertion at the end. 3. Returns the new length Mimics behavior by returning the updated length. -- Edge Cases Handled ✔ Works with multiple elements: ✔ Works with an empty array: ✔ Works with no arguments: (returns unchanged length) ✔ Appends elements in order This implementation is simple, efficient, and follows the behavior of exactly. -- <!-quiz-start --Q1: What does return? [ ] The pushed element(s) [ ] The original array [x] The new length of the array [ ] undefined Q2: How does the push polyfill add elements to the array? [ ] Using concat() [x] Assigning elements at index [ ] Using unshift() internally [ ] Creating a new array with spread operator Q3: What does return? [ ] [1, 2, 3, 4, 5] [ ] 3 [x] 5 [ ] [3, 4, 5] <!-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
15 of 24
LibraryJavaScriptPolyfills30 of 61

➕ Array.prototype.push() Polyfill

jspolyfillseasy

The push() method adds one or more elements to the end of an array and returns the new length.


✅ Implementation

Array.prototype.customPush = function (...elements) {
    for (let i = 0; i < elements.length; i++) {
        this[this.length] = elements[i]; // Add elements at the end of the array
    }
    return this.length; // Return the new length of the array
};

// Example usage:
const array = [1, 2, 3];
array.customPush(4, 5);

console.log(array); // Output: [1, 2, 3, 4, 5]
console.log(array.customPush(6, 7)); // Output: 7 (new length of array)
console.log(array); // Output: [1, 2, 3, 4, 5, 6, 7]

How It Works

  1. Uses the rest parameter (...elements)
    • Allows multiple arguments to be passed and handled dynamically.
  2. Iterates over new elements
    • Adds each element at this.length, ensuring insertion at the end.
  3. Returns the new length
    • Mimics push() behavior by returning the updated length.

Edge Cases Handled

✔ Works with multiple elements: customPush(4, 5, 6)

✔ Works with an empty array: [].customPush(1, 2, 3)

✔ Works with no arguments: array.customPush() (returns unchanged length)

✔ Appends elements in order

This implementation is simple, efficient, and follows the behavior of Array.prototype.push exactly.


Quick Quiz

Test your understanding with 3 quick questions

Q1What does `push()` return?
Q2How does the push polyfill add elements to the array?
Q3What does `[1, 2].customPush(3, 4, 5)` return?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❤️ by Tushar Khanna