Why double equals instead of triple?



Is there a reason that in React methods that rely ternary operators or if statements, the curriculum shows double equals instead of triple? I thought triple were generally best practice.


The double equals is coercive, and does not assume matching data types. If anything it uses more resources than the strict type matching ===, but in the case above the data may be loosely typed. This would have the effect of not throwing an exception if a different data type is passed into the comparison. It would just result in a return along the false branch of the ternary.


Hmm, what sort of example would the data not be a string?


Hypothetically speaking, mind. In this instance the object is hard coded, but in a dynamic situation the values may be set by a user or arrive from a data store. Loose typing in a ternary is a safeguard against an exception being thrown.


I think I follow. Maybe an example could be if for some reason a single-item array containing an identical string were passed in, you would want to be able to coerce that.


I’m afraid my explanation may not be doing this topic any justice. A mismatch of type will not throw an exception, but simply yield false (or the default branch of the ternary). Hopefully this has given you reason for thuoght and extra reading so you sort it out in your own mind.