CrackFrontendCF
Resources
Practice
CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna

CrackFrontendCF
Resources
Practice

πŸ” Array.prototype.includes() Polyfill

The includes() method checks if an array contains a value, returning true or false. Correctly handles NaN unlike indexOf().

The method determines whether an array includes a certain value, returning or . -- βœ… Implementation How It Works: 1. Parameters : : The element to search for in the array. : The index to start the search from (defaults to 0). Negative values are adjusted to count from the end of the array. 2. Iteration : The method iterates through the array starting from . 3. Comparison : For each element, it checks if the element is strictly equal () to or if both are . 4. Return Value : Returns if is found. Returns if is not found. Example: returns because is in the array. returns because is not in the array. returns because is found when counting from the end. returns because is found when counting from the end. returns because is found in the array. Key Features: Supports negative by adjusting it to count from the end of the array. Iterates through the array and checks for strict equality () with or if both are . Returns a boolean indicating whether is found in the array. -- <!-quiz-start --Q1: What does return? [x] true [ ] false [ ] undefined [ ] Throws an error Q2: How does differ from 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 return? [ ] true [x] false [ ] 2 [ ] 1 <!-quiz-end --
JavaScriptPolyfills
🎯 Array.prototype.at() Polyfill
medium
βœ… Array.prototype.every() Polyfill
medium
πŸ”² Array.prototype.fill() Polyfill
medium
πŸ” Array.prototype.filter() Polyfill
medium
πŸ”Ž Array.prototype.find() Polyfill
medium
πŸ”’ Array.prototype.findIndex() Polyfill
medium
πŸ”™ Array.prototype.findLast() Polyfill
medium
πŸ”™ Array.prototype.findLastIndex() Polyfill
medium
πŸ“‹ Array.prototype.flat() Polyfill
medium
πŸ” Array.prototype.includes() Polyfill
medium
πŸ”’ Array.prototype.indexOf() Polyfill
medium
βœ… Array.isArray() Polyfill
medium
πŸ—ΊοΈ Array.prototype.map() Polyfill
hard
βž– Array.prototype.pop() Polyfill
medium
βž• Array.prototype.push() Polyfill
easy
πŸ”„ Array.prototype.reduce() Polyfill
medium
πŸ”„ Array.prototype.reverse() Polyfill
hard
⬅️ Array.prototype.shift() Polyfill
hard
πŸ”˜ Array.prototype.some() Polyfill
medium
πŸ”€ Array.prototype.sort() Polyfill
hard
➑️ Array.prototype.unshift() Polyfill
hard
πŸ“ž Function.prototype.apply() Polyfill
medium
πŸ”— Function.prototype.bind() Polyfill
hard
πŸ“ž Function.prototype.call() Polyfill
medium
10 of 24
LibraryJavaScriptPolyfills25 of 61

πŸ” Array.prototype.includes() Polyfill

jspolyfillsmedium

The includes() method determines whether an array includes a certain value, returning true or false.


βœ… Implementation

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

How It Works:

  1. 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.
  2. Iteration : The customIncludes method iterates through the array starting from fromIndex.
  3. Comparison : For each element, it checks if the element is strictly equal (===) to searchElement or if both are NaN.
  4. 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.

Quick Quiz

Test your understanding with 3 quick questions

Q1What does `[NaN].customIncludes(NaN)` return?
Q2How does `includes()` differ from `indexOf()` when dealing with NaN?
Q3What does `[1, 2, 3].customIncludes(2, 2)` return?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna