Loading article...
The includes() method determines whether an array includes a certain value, returning true or false.
Array.prototype.customIncludes = function (searchElement, fromIndex = 0) { if (fromIndex < 0) { fromIndex = Math.max(this.length + fromIndex, 0); } for (let i = fromIndex; i < this.length; i++) { if (this[i] === searchElement || (Number.isNaN(this[i]) && Number.isNaN(searchElement))) { return true; } } return false; }; // Example usage: const array = [1, 2, 3]; console.log(array.customIncludes(2)); // Output: true console.log(array.customIncludes(4)); // Output: false console.log(array.customIncludes(3, -1)); // Output: true console.log(array.customIncludes(1, -3)); // Output: true console.log([NaN].customIncludes(NaN)); // Output: true
searchElement: The element to search for in the array.fromIndex: The index to start the search from (defaults to 0). Negative values are adjusted to count from the end of the array.customIncludes method iterates through the array starting from fromIndex.===) to searchElement or if both are NaN.true if searchElement is found.false if searchElement is not found.customIncludes(2) returns true because 2 is in the array.customIncludes(4) returns false because 4 is not in the array.customIncludes(3, -1) returns true because 3 is found when counting from the end.customIncludes(1, -3) returns true because 1 is found when counting from the end.[NaN].customIncludes(NaN) returns true because NaN is found in the array.fromIndex by adjusting it to count from the end of the array.===) with searchElement or if both are NaN.searchElement is found in the array.Test your understanding with 3 quick questions