JavaScript’s “double equals” operator, ==, is typically discouraged. And for good reason: its behavior is tricky. Where === asks &ld

Re-implementing JavaScript's == in JavaScript

submited by
Style Pass
2024-11-01 09:30:05

JavaScript’s “double equals” operator, ==, is typically discouraged. And for good reason: its behavior is tricky. Where === asks “are these the same thing?”, the double-equals operator asks a question that’s not straightforward.

More specifically, == implements something called the Abstract Equality Comparison Algorithm, a 13-step process for determining if two things are equivalent.

Let’s try to implement this algorithm in pure JavaScript. (Without using the == operator, of course.) This is mostly a useless idea, but I wanted to try it!

The spec says “the notation ‘Type(x)’ is used as shorthand for ’the type of x’”, where the types are “Undefined, Null, Boolean, String, Symbol, Number, BigInt, and Object”.

This is similar to the typeof operator, but Type(null) should be Null where typeof null is "object", and Type(function) should be Object where typeof myFunction is "function".

Leave a Comment
Related Posts