How to implement Array.prototype.includes? JavaScript Interview Question | Problem Solving | JavaScript Polyfills
@Yomesh Gupta

JavaScriptBeginner
398
Do you see value in our efforts?

Devtools Tech is a free platform for Frontend Engineers. It takes a lot of time, effort, and finanical resources to keep the platform running. If you see any value in our work and would like to support us then you can do using the following options.

InternationalDomestic

In this question, the candidate needs to implement a function customIncludes that mimics the behaviour of Array.prototype.includes method.

More about Array.prototype.includes

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Examples

const array = [5, 12, 8, 130, 44];
const found = array.customIncludes(5);

// prints true
console.print(found);

Syntax

The customIncludes takes two parameters

  1. searchElement

The value to search for.

  1. fromIndex (Optional)

The position in this array at which to begin searching for searchElement.

The first element to be searched is found at fromIndex for positive values of fromIndex, or at arr.length + fromIndex for negative values of fromIndex.

Defaults to 0.

Return value

A boolean value which is true if the value searchElement is found within the array (or the part of the array indicated by the index fromIndex, if specified).

array.customIncludes(searchElement);
array.customIncludes(searchElement, fromIndex);

Submission

Start the timer, complete your solution, and test it against the test cases provided by the platform. Ideally, you should finish this question within 30 mins. Share your solution with us -- https://twitter.com/devtoolstech

Loading IDE...