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.find() Polyfill

The find() method returns the first element satisfying a test function, or undefined if none found. Simpler than filter for single matches.

The method returns the first element in an array that satisfies a provided testing function. If no element matches, it returns . This polyfill supports the optional parameter for binding context. -- βœ… Implementation How It Works: 1. Iteration : The method iterates through the array using a loop. 2. Callback Execution : For each element, it calls the provided with the current element, its index, and the array. If the callback returns a truthy value, the method immediately returns that element. 1. Optional : If provided, is used to bind inside the callback function using . 2. No Match : If no element satisfies the condition in , it returns . Example: returns the first element greater than 10, which is . If no element matches, such as , it returns . Why Use ? Explicit Binding : The ensures that the value inside the callback is explicitly set to . This is crucial when is provided and the callback relies on it. Without , the value could be undefined (in strict mode) or the global object (non-strict mode), leading to unexpected results. By using , we guarantee that is used as the context inside the callback. Example of in Action: Summary: *allows explicit control over the context inside the callback. Without , the context inside the callback may not behave as expected, especially when is provided. -- <!-quiz-start --Q1: What does return? [ ] 130 [x] 12 [ ] [12, 130] [ ] 1 Q2: What does return when no element matches? [ ] null [ ] -1 [ ] false [x] undefined Q3: Why is used in the find polyfill? [ ] To improve performance [ ] To handle async callbacks [x] To explicitly set the context inside the callback [ ] To convert the callback to an arrow function <!-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
5 of 24
LibraryJavaScriptPolyfills20 of 61

πŸ”Ž Array.prototype.find() Polyfill

jspolyfillsmedium

The find() method returns the first element in an array that satisfies a provided testing function. If no element matches, it returns undefined. This polyfill supports the optional thisArg parameter for binding context.


βœ… Implementation

Array.prototype.customFind = function(callbackFn, thisArg) {
    // Iterate through the array and execute the callback function
    for (let i = 0; i < this.length; i++) {
        // Call the callback function with the current element, index, and array
        // .call() is used to explicitly set 'this' inside the callback
        if (callbackFn.call(thisArg, this[i], i, this)) {
            return this[i]; // Return the first matching element
        }
    }

    return undefined; // Return undefined if no element matches
};

// Example usage:
const array = [5, 12, 8, 130, 44];

// Using customFind to find the first element greater than 10
const found = array.customFind(element => element > 10);

console.log(found); // Output: 12

How It Works:

  1. Iteration : The customFind method iterates through the array using a for loop.
  2. Callback Execution : For each element, it calls the provided callbackFn with the current element, its index, and the array.
  • If the callback returns a truthy value, the method immediately returns that element.
  1. Optional thisArg : If provided, thisArg is used to bind this inside the callback function using .call(thisArg, this[i], i, this).
  2. No Match : If no element satisfies the condition in callbackFn, it returns undefined.

Example:

  • customFind(element => element > 10) returns the first element greater than 10, which is 12.
  • If no element matches, such as customFind(element => element > 200), it returns undefined.

Why Use .call(thisArg, ...)?

  • Explicit this Binding : The .call(thisArg, ...) ensures that the this value inside the callback is explicitly set to thisArg. This is crucial when thisArg is provided and the callback relies on it. Without .call(), the this value could be undefined (in strict mode) or the global object (non-strict mode), leading to unexpected results. By using .call(thisArg, ...), we guarantee that thisArg is used as the context inside the callback.

Example of .call() in Action:

function printThis() {
    console.log(this); // Logs the value of `this`
}

const obj = { name: 'Alice' };

// Using `.call()` to set `this` to `obj`
printThis.call(obj); // Logs: { name: 'Alice' }

Summary:

  • .call(thisArg, ...) allows explicit control over the this context inside the callback.
  • Without .call(), the this context inside the callback may not behave as expected, especially when thisArg is provided.

Quick Quiz

Test your understanding with 3 quick questions

Q1What does `[5, 12, 8, 130].customFind(x => x > 10)` return?
Q2What does `find()` return when no element matches?
Q3Why is `.call(thisArg, ...)` used in the find polyfill?

Continue Reading

CrackFrontend

Your guide to mastering frontend interviews

Resources

LibraryPracticeDonate

Company

About UsContact

Legal

Privacy PolicyTerms of Service

Built with ❀️ by Tushar Khanna