🔍 Array.prototype.includes() Polyfill
Category: js / polyfills
Difficulty: medium
The includes() method determines whether an array includes a certain value, returning true or false. ✅ Implementation [code example] How It Works: Parameters : 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. Iteration : The customIncludes method iterates through the array starting from fromIndex. Comparison : For each element, it checks if the element is strictly equal (===) to searchElement or if both are NaN. Return Value : Returns true if searchElement is found. Returns false if searchElement is not found. Example: 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. Key Features: Supports negative fromIndex by adjusting it to count from the end of the array. Iterates through the array and checks for strict equality (===) with searchElement or if both are NaN. Returns a boolean indicating whether searchElement is found in the array. <!-- quiz-start --> Q1: What does [NaN].customIncludes(NaN) return? [x] true [ ] false [ ] undefined [ ] Throws an error Q2: How does includes() differ from indexOf() when dealing with NaN? [ ] They behave the same way [x] includes() correctly finds NaN, indexOf() returns -1 [ ] indexOf() finds NaN, includes() returns false [ ] Neither can find NaN Q3: What does [1, 2, 3].customIncludes(2, 2) return? [ ] true [x] false [ ] 2 [ ] 1 <!-- quiz-end -->...