DEV Community

Cover image for Ace Your JS Interview: How to Implement a Polyfill for the some() Method
Imran shaikh
Imran shaikh

Posted on • Edited on

Ace Your JS Interview: How to Implement a Polyfill for the some() Method

One of the most common questions in senior JavaScript interviews is asking a candidate to write a polyfill. It’s not enough to just know how to use built-in array methods; you need to understand how they work under the hood.
In my latest YouTube tutorial, I broke down exactly how to build a custom implementation of the some() function. If you are preparing for a coding interview or just want to strengthen your logic building, this guide is for you.

Youtube Link

What is the some() Method?

Before we build it, we must understand what it does. As explained in the video, the some() function checks an array to see if at least one element satisfies a specific condition.
It works on a simple boolean logic:
• If one or more elements meet the condition, it returns true.
• If no elements meet the condition, it returns false.
For example, if we have an array of mixed negative and positive numbers [-2, 0, 3, -1, 5, -4], and we want to check if there is any positive number, some() will iterate through the list. As soon as it finds 3 (which is greater than 0), it stops looking and returns true.

Step 1: Setting up the Prototype

To make our custom function available to all arrays—just like the native .some() method—we need to attach it to the Array.prototype.
In the video, we define our custom method (let’s call it mySome) on the prototype so that any array object can call it directly.
Array.prototype.mySome = function(callback) {
// Logic goes here
}

By using Array.prototype, we ensure that the function is accessible to any array instance, allowing us to use the this keyword to access the data.

Step 2: Handling Errors

A good polyfill is robust. Before processing the logic, we must ensure the user has actually passed a function as an argument.
If the typeof the callback is not a function, we should throw an error immediately: "Callback should be a function". This prevents the code from crashing unexpectedly later on.

Step 3: The Logic (The this Keyword)

Inside our prototype function, the array we are operating on is accessible via the this keyword. For clarity, we can assign this to a variable, though we can also access it directly.
We then need to loop through the array. The standard some() method accepts a callback that takes three arguments:

  1. Item: The current element.
  2. Index: The current index.
  3. Self: The array itself. Here is the core logic we implemented in the tutorial:
Array.prototype.mySome = function (callback, thisArg) {
    if (typeof callback !== "function") {
        throw new TypeError("Callback must be a function");
    }
    console.log(this);
    console.log(thisArg);
    for (let i = 0; i < this.length; i++) {
        // 🔥 Skip sparse (empty) indexes
        if (i in this) {
            if (callback.call(thisArg, this[i], i, this)) {
                return true;
            }
        }
    }
    return false;
};
Enter fullscreen mode Exit fullscreen mode

As demonstrated in the video, the moment the logic finds a "truthy" value (like the number 3 in our positive number check), it returns true and exits the loop. If it iterates through the whole array without success, it defaults to false.
Testing the Polyfill
Does it work? In the video, we test this against a real scenario: checking for values greater than zero.

const numbers = [-2, 0, 3, -1, 5, -4];

const result = numbers.mySome((element) => {
    return element > 0;
});
console.log(result); // Output: true
Enter fullscreen mode Exit fullscreen mode

Because the logic encountered 3 and 5, which are positive, our polyfill correctly identified that the condition was met.
Watch the Full Implementation
Writing polyfills is a fantastic way to master JavaScript fundamentals. If you want to see this code being written line-by-line, along with a detailed explanation of how the call stack and callbacks work in real-time, check out my full video tutorial below.
I cover the edge cases and debugging steps that I couldn't fit into this article!

If you found this guide helpful, please follow me on dev.to and subscribe to my YouTube channel for more JavaScript interview prep!

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Your implementation of some would fail an interview. It doesn't handle sparse arrays correctly. It's not a valid polyfill for some as it behaves differently to the native version:

const myArr = [ 1, , 3 ]
const test = x => x === undefined
console.log(myArr.some(test)) // false
console.log(myArr.mySome(test)) // true
Enter fullscreen mode Exit fullscreen mode
Collapse
 
imranmind profile image
Imran shaikh

Thanks for your feedback Jon, I have corrected