Rikudou_Sage
link
fedilink
English
11Y

Doesn’t it widen the types regardless of position? Meaning 1 == “1” will be compared as strings, not numbers.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality

mdn goes into it more and it’s way more involved than I thought, looks like order of operand doesn’t matter. see the number to string section

Rikudou_Sage
link
fedilink
English
11Y

It seems it is that way, which is weird. You should always convert to the widest type, meaning string for comparing numbers and strings. I just checked that 1 == "01" is true, which means that “01” gets cast to an integer. And according to the document it might be that for example 1 == "a" would basically be interpreted as 1 === NaN which is false.

Create a post

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 120 users / day
  • 257 users / week
  • 744 users / month
  • 3.72K users / 6 months
  • 1 subscriber
  • 1.48K Posts
  • 32.6K Comments
  • Modlog