9

Are there any cases where

x == y //false
x === y //true

is that ever possible in JS? :)

user2357112
  • 260,549
  • 28
  • 431
  • 505
Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 2
    I wouldnt think so. But then again, javascript is never short of surprises – AmmarCSE May 22 '15 at 19:57
  • 1
    if the first line changed `x` or `y` (using a mutating getter or even an overloaded valueOf), then it could happen, but on it's face, no. – dandavis May 22 '15 at 20:18
  • the most illustrative and educational way to see that it is not possible is to look at this table https://dorey.github.io/JavaScript-Equality-Table/ – jJ' May 25 '15 at 03:55

4 Answers4

10

No. That's never possible. === checks for type and equality. == just checks for equality. If something isn't == it can never be ===.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
8

It'd be impossible. == compares value, while === compares value and type. Your case would require an impossible condition.

a === b -> (typeof(a) == typeof(b)) && (value(a) == value(b))
a == b ->  (value(a) == value(b))

You couldn't have the value comparisons in the == case be true while requiring the exact same comparison in === become false.

Marc B
  • 356,200
  • 43
  • 426
  • 500
3

== - Returns true if the operands are equal.

=== - Returns true if the operands are equal and of the same type.

So, I'll say not possible.

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
2

Briefly, if === is true, then == will return true. If === returns false, then == may or may not return false.

Examples:

5===5 is true, which means that 5==5 also must be true.

'5'===5 is false, and '5'==5 is true.

'6'===5 is false, and '6'==5 is also false.

This behavior is because a===b checks to make sure that the value and type of a and b are equal, while a==b only checks to make sure that their values are equal.

Frank Bryce
  • 8,076
  • 4
  • 38
  • 56