JS Built-ins
← Array26 / 226
ArrayArray.isArrayadvanced

Array.isArray() β€” distinguish array-like objects

πŸ“– Usage Example
Array.isArray(Array.from('abc'))  // β†’ true  (converted to real array)
Array.isArray({0:'a', length:1})  // β†’ false (array-like, not array)

Use Array.isArray() to distinguish real arrays from array-like objects such as NodeList or arguments.

Array.isArray() β€” arrays vs array-like objects

An array-like object has a numeric length property (e.g. { length: 2 }, arguments, NodeList) but is not a real array. Array.isArray returns false for them.

Challenge: Implement getType that classifies a value as:

  • 'array' β€” a real array
  • 'array-like' β€” a non-array object with a numeric length property
  • 'other' β€” anything else
getType([1, 2])       // β†’ 'array'
getType({ length: 2}) // β†’ 'array-like'
getType('hello')      // β†’ 'other'
πŸ’‘ Hints (2)
  • β€’ Check `Array.isArray` first β€” even a subclass array should return `"array"`.
  • β€’ For "array-like": the value must be an object (not null) with a numeric `length` property.