Weird JavaScript: How Equals Operator Works

Hi, friends. As you know JavaScript is a kind of weird language, at least it seems like for the first time. And what I am going to do is to improve your impression with this category of articles. So welcome to the Weird JavaScript.

And in this first post we are going to talk about equals operator (==). Is there anything can be unclear here? Definitely, everything is understandable for you, just make sure you can answer this questions without any hesitation.

1 == '1'
[] == false
false == {}
NaN == NaN
+0 == -0
null == null
null == undefined
[] == []

If you are not sure in your answers or you just want to get the results, this short post is worth your attention.

To make long story short, let’s take a look at the abstract comparison algorithm which I made for you in a form of a graph. But pay attention that you can switch parameters on any step.

Abstract Equality Comparison Algorithm JavaScript

For now, I suppose, everything is understandable for you except ToNumber and ToPrimitive functions. But there nothing difficult in them too.

ToNumber function

There are a lot of ways how to convert anything to a number in javascript. That is why we use ToNumber which is an abstract function that converts its argument to a value of type Number.
undefined becomes NaN
null becomes 0
true becomes 1
false becomes 0

Strings with mathematical values become these number values:
'0' becomes 0
'42' becomes 42
'-100' becomes -100
'[space]0.01' becomes 0.01
0xA becomes 10

Any spaces or empty string become 0 and other strings become NaN.

ToPrimitive function

ToPrimitive is also an abstract function which converts input argument to primitive type.
1. In general, if object has valueOf method and it returns a primitive, then return it.
2. Otherwise, if object has toString method and it returns a primitive, then return it.
3. Otherwise trow an exception.

true examples:

'[object Object]' == {}   
42 == { valueOf: () => 42 }
42 == { valueOf: () => 42, toString: () => 0 }
0 == { toString: () => '0' }

exception example:

0 == Object.create(null)

Wrap up

Looks like it is everything you need to know about equals operator. To dig a bit deeper, take a look at ECMAScript Language Specification.

Hope you liked this post and thank you for reading my blog :)


5 thoughts on “Weird JavaScript: How Equals Operator Works

    1. Thank you for your comment and yes, you can also use strict equals operator (===) which works the same but returns false when types are different. Definitely, you may use any of them depending on the way of how you are going to compare, but it is a common practice to use strict equals because it increases the clarity of your code and prevents any implicit conversions.

      Liked by 1 person

    1. Sure.
      1. false to number (0)
      2. [] to Primitive (.valueOf() does not return primitive, so .toString() returns “”)
      3. “” to number (0)
      4. 0 euqals to 0, so return true


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s