Loading article...
The push() method adds one or more elements to the end of an array and returns the new length.
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]
...elements)
element at this.length, ensuring insertion at the end.push() behavior by returning the updated length.✔ 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.
Test your understanding with 3 quick questions