Alexander Mills
Alexander Mills

Reputation: 100250

Can === hold when == doesn't?

Are there any cases where

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

is that ever possible in JS? :)

Upvotes: 9

Views: 210

Answers (4)

Frank Bryce
Frank Bryce

Reputation: 8446

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.

Upvotes: 2

Arup Rakshit
Arup Rakshit

Reputation: 118289

== - 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.

Upvotes: 3

Marc B
Marc B

Reputation: 360782

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.

Upvotes: 8

Justin Niessner
Justin Niessner

Reputation: 245479

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

Upvotes: 10

Related Questions