What does the following code demonstrate? const brand = Symbol(‘brand’); class MyArray extends Array { static [Symbol.hasInstance](instance) { return Array.isArray(instance); } } console.log([] instanceof MyArray);

JavaScript Professional Hard

JavaScript Professional — Hard

What does the following code demonstrate? const brand = Symbol(‘brand’); class MyArray extends Array { static [Symbol.hasInstance](instance) { return Array.isArray(instance); } } console.log([] instanceof MyArray);

Key points

  • Symbol.hasInstance allows customizing instanceof behavior
  • MyArray class extends Array and defines Symbol.hasInstance method
  • The method returns true for arrays, making [] an instance of MyArray

Ready to go further?

Related questions