0

While writing a sudoku solver, I ran into a issue which took me hours to solve which is based on ['1','2','3'].includes(3). is false. Since 3 in includes is a type number but 3 in array is type string. But this is against the intuition that type coercion happens while comparing values in js? Should it be corrected in js? ex: ['1','2','3'].includes(3) // false and ['1','2','3'].includes('3') // true

Throw some light on whether this is needed or should be fixed as it will lead to errors in programs if not checked carefully.

  • 1
    "*type coercion happens while comparing values in js*" - no, not always. `'3' === 3` is false as well. – Bergi Nov 04 '22 at 04:24
  • Yes. But I thought it is very common. Most developers will overlook this and assumes type coercion happens in .includes and will lead to bugs. – Sathish Kannan Nov 04 '22 at 04:25
  • 1
    Well, that's on them, not on `includes`. You should never write code that relies on comparison between different types. – Bergi Nov 04 '22 at 04:27

0 Answers0