✅ Array.isArray() Polyfill
Category: js / polyfills
Difficulty: medium
The Array.isArray() method determines whether the passed value is an Array. ✅ Implementation [code example] Explanation: Null and Undefined Check : The function first checks if value is null or undefined. If it is, it immediately returns false. 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]". 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. <!-- quiz-start --> Q1: Why is Object.prototype.toString.call() used instead of typeof? [ ] 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 customIsArray({ length: 3 }) return? [ ] true [x] false [ ] undefined [ ] Throws an error Q3: What string does Object.prototype.toString.call([]) return? [ ] "[object Object]" [x] "[object Array]" [ ] "Array" [ ] "[]" <!-- quiz-end -->...