Loading article...
The some() method tests whether at least one element in the array passes the test implemented by the provided function.
Array.prototype.customSome = function (callback, thisArg) { if (typeof callback !== 'function') { throw new TypeError(callback + ' is not a function'); } for (let i = 0; i < this.length; i++) { if (callback.call(thisArg, this[i], i, this)) { return true; // Return true immediately if a match is found } } return false; // Return false if no element satisfies the condition }; // Example usage: const array = [1, 2, 3, 4, 5]; const even = (element) => element % 2 === 0; console.log(array.customSome(even)); // Output: true console.log(array.customSome((num) => num > 10)); // Output: false
callback is a function
TypeError (same behavior as Array.prototype.some).callback for each element with (element, index, array)..call(thisArg, ...) to bind the optional thisArg.callback returns true, customSome immediately returns true.false if no match is found
customSome returns false after the loop.β Empty array always returns false
β Works with thisArg binding
β Stops checking as soon as one match is found (Optimized)
β Throws an error if callback is not a function
This version is simple, efficient, and follows the behavior of Array.prototype.some exactly.
Test your understanding with 3 quick questions