JS Built-ins
← Array11 / 226
ArrayArray[Symbol.species]advanced

Array[Symbol.species] β€” Array.isArray on subclass result

πŸ“– Usage Example
class MyArr extends Array {}
const a = new MyArr(1, 2, 3)
console.log(a instanceof MyArr)   // β†’ true
console.log(a instanceof Array)   // β†’ true

Subclasses of Array are instances of both the subclass and Array due to prototype chain.

Array.isArray() with subclass instances

Array.isArray() returns true for any object that is an exotic Array object β€” including subclass instances. This means a class that extends Array produces instances that pass Array.isArray().

Challenge: Create SpecialArray that extends Array. Implement checkSubclassIsArray that returns true when Array.isArray reports true for a SpecialArray instance, even though it is not an instance of plain Array constructed directly.

checkSubclassIsArray() // β†’ true
πŸ’‘ Hints (2)
  • β€’ `Array.isArray` checks the internal [[IsArray]] slot, which subclass instances have.
  • β€’ You can use both `Array.isArray` and `instanceof` to understand subclass arrays.