cft

Advanced Javascript Concepts: Type Coercion

A few notes on type coercion


user

Allan Sendagi

a year ago | 2 min read

Type coercion in Javascript is ridiculous. It's frustrating.

It’s something we should know and understand how it works but we shouldn't get so hung up on. I think it shouldn’t be in your code.

So what is type coercion?

It’s something like this:

Simply, when the operands are different types, for example, in the case above, a number and a string, one of them will be converted into an equivalent value by the Javascript engine.

So when the Javascript engine sees 1== ‘1’, it will assume that you mean the number 1. Therefore, it's going to compare the number 1 to number 1.

Therefore we can conclude that type coercion happens when a language converts one type to another.

Therefore we can conclude that type coercion happens when a language converts one type to another.

Do all languages have Type Coercion?

Yes.

All languages have type coercion because we always need to convert types between programs to do things.

In memory, the different types look completely different from what we type on our machines. The number four, for example, in memory on my computer is represented by 1's and 0’s.

At different levels of the stack, there is some sort of type coercion happening.
And since Javascript is dynamically typed, it has heavy type coercion.

In Javascript, type coercion happens when you use the double equals.
The double equal sign simply means — compare the two values and if they have different types, try to coerce one into a different type such as
 1== ‘1’.

Now if we do 1=== ‘1’. This is three equals; when I run this I get false. Three equals in Javascript means compare two values but don't try to coerce the values. Be explicit with your comparison.

So when should you use two equals instead of three equals?

The answer I would say is Never!

Now some people argue that double equals have some interesting applications where we coerce something and do some sort of checking quickly.

Unfortunately, that's not predictable code. It can be confusing and it can trick some people. So always use three equals instead of two.

Type coercion doesn't only happen with equal signs. You can also do an if statement.

For example:

Here Javascript coerces to equal to true.

And if I do:

Here Javascript coerces 0 into false.

With this sort of type of coercion, there are a lot of specifics to it can get really frustrating.

source: https://dorey.github.io/JavaScript-Equality-Table/

From the table, you can see that things only make sense with the 3 equals.
Once we get to double equals, it’s bonkers. For example:

And there are so many weird edge cases here.

Object.is

From the MDN page above, we can see that from the equality comparisons ===, ==, we also have something called Object.is.

I haven't seen this much out there. So what is it?

In javascript, there is a concept of -0 and +0.
For example:

Technically these are different things in Javascript.

Because if I do:

Why is this useful?

We see that Object.is works the same as triple equals except for a few cases where there is +0 and -0 and NaN.

Chances are slim that you will encounter such a problem. But it’s good to know how confusing type coercion can be.

You can read more about this if you are interested here.

The main takeaway for us here is you should use triple equals because type Coercion in Javascript is tricky.

Here are a few examples to blow your mind.

false == “”

false == []

false == {}

“” == 0

“” == []

“” == {}

0 == []

0 == {}

0 == null

Upvote


user
Created by

Allan Sendagi

Technology will save homo sapiens from extinction


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles