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

The isArray() method reliably determines if a value is an array. More accurate than typeof for cross-frame array detection.

The method determines whether the passed value is an Array. -- ✅ Implementation Explanation: 1. Null and Undefined Check : The function first checks if is or . If it is, it immediately returns . 2. Reliable Type Checking : For all other values, the function uses . This method returns a string in the format , where is the internal class of the object. For arrays, it returns . 3. Why This Works : is a reliable way to determine the internal class of an object, which helps accurately identify arrays. This is a cross-environment solution that works similarly to . Behavior: Returns for arrays : Works for empty arrays, populated arrays, arrays created with the constructor, and even . Returns for non-arrays : Includes , , objects, numbers, strings, booleans, and array-like objects such as . -- <!-quiz-start --Q1: Why is used instead of ? [ ] It's faster [ ] typeof works fine for arrays [x] typeof returns "object" for arrays, which is not specific enough [ ] It handles null values better Q2: What does return? [ ] true [x] false [ ] undefined [ ] Throws an error Q3: What string does return? [ ] "[object Object]" [x] "[object Array]" [ ] "Array" [ ] "[]" <!-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
12 of 24
LibraryJavaScriptPolyfills27 of 61

✅ Array.isArray() Polyfill

jspolyfillsmedium

The Array.isArray() method determines whether the passed value is an Array.


✅ Implementation

function customIsArray(value) {
    // Return false if the value is null or undefined
    if (value === null || value === undefined) {
        return false;
    }

    // Use Object.prototype.toString to reliably check the type of value
    // This method returns '[object Array]' for arrays
    return Object.prototype.toString.call(value) === '[object Array]';
}

Explanation:

  1. Null and Undefined Check : The function first checks if value is null or undefined. If it is, it immediately returns false.
  2. Reliable Type Checking : For all other values, the function uses Object.prototype.toString.call(value). This method returns a string in the format "[object Type]", where Type is the internal class of the object. For arrays, it returns "[object Array]".
  3. Why This Works : Object.prototype.toString is a reliable way to determine the internal class of an object, which helps accurately identify arrays. This is a cross-environment solution that works similarly to Array.isArray().

Behavior:

  • Returns true for arrays : Works for empty arrays, populated arrays, arrays created with the Array constructor, and even Array.prototype.
  • Returns false for non-arrays : Includes undefined, null, objects, numbers, strings, booleans, and array-like objects such as TypedArrays.

Quick Quiz

Test your understanding with 3 quick questions

Q1Why is `Object.prototype.toString.call()` used instead of `typeof`?
Q2What does `customIsArray({ length: 3 })` return?
Q3What string does `Object.prototype.toString.call([])` 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