Loading article...
The every() method tests whether all elements in the array pass the test implemented by the provided function.
Array.prototype.customEvery = 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 false; // Return false immediately if one element fails } } return true; // Return true if all elements pass the test }; // Example usage: const input = [2, 4, 6, 8]; const isEven = (element) => element % 2 === 0; console.log(input.customEvery(isEven)); // Output: true console.log([2, 4, 5, 8].customEvery(isEven)); // Output: false console.log([].customEvery(isEven)); // Output: true (empty array always returns true)
callback is a function
TypeError (same behavior as Array.prototype.every).callback for each element with (element, index, array)..call(thisArg, ...) to bind thisArg if provided.callback returns false for any element, customEvery immediately returns false (optimized).true if all tests pass
true.✔ Empty array always returns true
✔ Stops checking as soon as one element fails (Optimized)
✔ Works with thisArg binding
✔ Throws an error if callback is not a function
This implementation is efficient, clean, and mirrors Array.prototype.every exactly.
Test your understanding with 3 quick questions