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.
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:
- Item: The current element.
- Index: The current index.
- Self: The array itself. Here is the core logic we implemented in the tutorial:
Array.prototype.mySome = function(callback) {
// Error handling
if (typeof callback !== 'function') {
throw new Error("Callback should be a function");
}
// Iterate through the array
for (let i = 0; i < this.length; i++) {
// If the callback returns true for any element...
if (callback(this[i], i, this)) {
return true; // ...return true immediately
}
}
// If the loop finishes without finding a match
return false;
};
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
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 (1)
Your implementation of
somewould fail an interview. It doesn't handle sparse arrays correctly.