Implementing Array.prototype.includes in JavaScript
The Array.prototype.includes() method determines whether an array contains a specific element. This is a fundamental array method used frequently for searching and validation, and understanding its implementation is a great exercise in JavaScript fundamentals. Your task is to implement this method as an extension to the Array prototype.
Problem Description
You need to implement the includes() method directly on the Array.prototype. This method should take two arguments:
searchElement: The value to search for within the array.fromIndex(optional): The index to start the search from. If omitted, the search starts from the beginning of the array (index 0).
The method should return true if the searchElement is found within the array (starting from fromIndex), and false otherwise. The comparison should use strict equality (===).
Key Requirements:
- The implementation must be added to
Array.prototype. - The method should handle the optional
fromIndexargument correctly. - The method should use strict equality (
===) for comparison. - The method should return
trueif the element is found, andfalseif it is not. - The method should handle edge cases gracefully (see below).
Expected Behavior:
- If
fromIndexis positive and within the bounds of the array, the search should begin at that index. - If
fromIndexis negative, it should be treated as an offset from the end of the array. For example,fromIndexof -1 should start the search from the last element. IffromIndexis less than-array.length, it should be treated as 0. - If
fromIndexis greater than or equal to the array's length, the search should start from the end of the array.
Edge Cases to Consider:
- Empty arrays.
searchElementbeingNaN. (Note:NaN === NaNis always false, soincludes(NaN)should always returnfalseregardless of the array contents).fromIndexbeing out of bounds (negative or positive).- Arrays containing different data types.
Examples
Example 1:
Input: [1, 2, 3, 4, 5].includes(3)
Output: true
Explanation: The array contains the value 3.
Example 2:
Input: [1, 2, 3, 4, 5].includes(6)
Output: false
Explanation: The array does not contain the value 6.
Example 3:
Input: [1, 2, 3, 4, 5].includes(3, 2)
Output: true
Explanation: The array contains the value 3, starting the search from index 2.
Example 4:
Input: [1, 2, 3, 4, 5].includes(3, -1)
Output: false
Explanation: The array does not contain the value 3, starting the search from the last element (index 4).
Example 5:
Input: [1, 2, NaN].includes(NaN)
Output: false
Explanation: NaN is never strictly equal to itself, so includes(NaN) always returns false.
Constraints
- The input array can contain any data type.
searchElementcan be any data type.fromIndexmust be an integer.- The implementation should be efficient enough to handle arrays of up to 10,000 elements without significant performance degradation.
Notes
Consider using a for loop or for...of loop to iterate through the array. Pay close attention to how you handle the fromIndex parameter, especially negative values and out-of-bounds scenarios. Remember to use strict equality (===) for comparisons. Think about how to handle the edge case of NaN correctly.