What is the output of the following code? const obj = { [Symbol.toPrimitive](hint) { if (hint === ‘default’) return ‘default’; return 42; } }; console.log(obj == ‘default’);

JavaScript Professional Hard

JavaScript Professional — Hard

What is the output of the following code? const obj = { [Symbol.toPrimitive](hint) { if (hint === ‘default’) return ‘default’; return 42; } }; console.log(obj == ‘default’);

Key points

  • The 'default' hint is triggered when using == with a string
  • The Symbol.toPrimitive method is invoked in this comparison
  • The return value of the method is compared to the string
  • This comparison results in true because the values match

Ready to go further?

Related questions