0

I have code that gets the users locale and store it in a variable. I then have a conditional that checks if the locale matches a specific value - see the picture below:

enter image description here

Above you can see the value of locale is "sv-SE".

I then have a conditional too see if the value of locale is "en-US" || "en-GB" in a if statement.

I would except the statement to be false, since the value of locale is "sv-SE" in this case, and does not match the string values in the statement - but the if conditional becomes true and the code inside the if statement executes. Why?

Arte2
  • 309
  • 4
  • 19
  • 2
    `x == "a" || "b"` should be `x == "a" || x == "b"`. Having a non-empty string on its own in a conditional will always be `true` – DBS Nov 03 '20 at 17:22
  • 2
    Please provide code as text, not as a picture of text. – Heretic Monkey Nov 03 '20 at 17:23
  • 1
    `"en-GB"` is a truthy value so your if-statement will always match, since it doesn't compare against anything variable. – 3limin4t0r Nov 03 '20 at 17:23
  • 1
    When you do `"en-US" || "en-GB"`, the outcome here is always `en-US`. OR returns the first truthy value or the last one if no truthy value is found. So your comparison is always doing `if(locale === "en-US")` – kimobrian254 Nov 03 '20 at 17:32

1 Answers1

1

You missed the comparison to the locale variable... Try to do this and see if it works.

if (locale === 'en-US' || locale === 'en-GB') {
   ...
}
Francisco
  • 1,748
  • 1
  • 17
  • 19