0

While solving one issue, I notice following condition

if("String" == ["String"]) {
    console.log("Valid");
}
else
{
    console.log("Invalid");

}

I thought that it should log "Invalid", since first value is string and second value is object of string. But to my surprise, it is logging "Valid". Any specific reason, why this condition is true.

Pointy
  • 405,095
  • 59
  • 585
  • 614
ndNextLearn
  • 11
  • 1
  • 5
  • In JavaScript, you should always use the [strict equality](https://javascript.info/comparison#strict-equality) in caparison. – Megrax May 28 '22 at 17:36

1 Answers1

1

You're using == to compare them, so the array is first converted to a string.

If you use === (which you probably should do always), they'll not be equal.

Pointy
  • 405,095
  • 59
  • 585
  • 614