Loading article...
The Array.isArray() method determines whether the passed value is an Array.
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]'; }
value is null or undefined. If it is, it immediately returns false.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]".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().true for arrays : Works for empty arrays, populated arrays, arrays created with the Array constructor, and even Array.prototype.false for non-arrays : Includes undefined, null, objects, numbers, strings, booleans, and array-like objects such as TypedArrays.Test your understanding with 3 quick questions