Overlord
Overlord

Reputation: 2906

figuring out javascript equality operator

While trying to fully understand the difference between equality operator and identity operator, I came across an article at MSDN that explains what they both do, in terms of their inner workings, but I still had a few doubts and decided to create a flowchart so I could have a better picture. Now my question is, is this flowchart correct? or am I missing something?

It's also my understanding that the identity operator (===) would work pretty much the same way, but without attempting to convert A and B to boolean, number or string, in the first step. Is that correct?

You can see the image here too:

enter image description here

Ok here is the real thing, it was a matter of principles ;)

enter image description here

Upvotes: 8

Views: 294

Answers (2)

Bergi
Bergi

Reputation: 664548

is this flowchart correct?

No. Apart from being layouted terrible, it is misleading and partially wrong.

Am I missing something?

Yes. The first step, "try to convert A and B to boolean, string or number" is wrong - that's not the first step in the equality comparison algorithm. Also, when to convert which of the variables to which type?

Then, the next step should be a type distinction, instead of repeatedly asking for identical values of a specific type.

The "last" step "Can they (the types) be coerced into any of the last 5 situations? -> Coerce types" lacks detail. All the detail. Which is the most relevant part of the sloppy equality comparison:

  • Which types can be coerced?
  • What types would be coerced to which?
  • How does the coercion of the values work?

And no, after the coercion the algorithm pretty much starts at the beginning, not with the question about strings.

It's also my understanding that the identity operator (===) would work pretty much the same way, but without attempting to convert A and B to boolean, number or string, in the first step.

That first step is not apparent in the actual algorithm, so No. In fact, === works the same except the last step, which coerces values into other types - instead, false is returned.


Edit: Your second diagram is accurate (correct), although it still features some odd layout decisions.

Upvotes: -2

RobG
RobG

Reputation: 147403

is this flowchart correct?

No. You should use the ECMAScript specification for the Abstract Equality Comparison Algorithm to create the flowchart. ToBoolean is certainly not the first step (it's not used in any step).

or am I missing something?

Yes, a lot.

It's also my understanding that the identity operator (===) would work pretty much the same way, but without attempting to convert A and B to boolean, number or string, in the first step. Is that correct?

The Strict Equality Comparison Algorithm is almost identical to the Abstract Equality Comparison Algorithm, there is a difference only if the argument Types are different, and in that case there is a precise order in which the Types are made equal before the comparison is made.

Upvotes: 4

Related Questions