JS Built-ins
← Array23 / 226
ArrayArray.isArraybeginner

Array.isArray() β€” why not typeof

πŸ“– Usage Example
typeof []          // β†’ 'object'  (misleading)
Array.isArray([])  // β†’ true      (accurate)

Prefer Array.isArray() over typeof for array detection β€” typeof returns 'object' for arrays.

Array.isArray() vs typeof

typeof [] returns 'object' β€” the same as for plain objects, null, and other non-primitives. Array.isArray() was introduced specifically to distinguish real arrays from objects.

Challenge: Implement isRealArray that returns true only for real arrays (not other objects) by using Array.isArray.

isRealArray([])           // β†’ true  (typeof [] === 'object' too!)
isRealArray({})           // β†’ false
isRealArray(null)         // β†’ false (typeof null === 'object' β€” gotcha!)
πŸ’‘ Hints (2)
  • β€’ `typeof null === "object"` is a famous JavaScript quirk β€” always use `Array.isArray` for arrays.
  • β€’ `typeof []` also returns `"object"`, making `typeof` useless for this check.