Coercion in JavaScript is a feature in JavaScript language, which is simply forcefully converting a data type to another data type based upon a predefined precedence table (listed in Mozilla I think go figure haha).

JavaScript implements dynamic typing to define it’s variables. So when you pass two different data types into an operator, it won’t necessarily throw an error.

Example:

"2" < 0 # -> false

This should be a strange event if you are new to programming in general or coming from some other programming paradigm which has static typing like C or Java. The case is more interesting when you consider this particular example:

"2" == 2 # -> true

Whereas,

"2" === 2 # -> false

Popular explanation I have heard on this is === operator compares the data type along with the values. Thus, returning false upon it’s deployment. Which makes sense.

But what is really happening under the hood is that, JavaScript engine coerces the "2" in first example to 2 of integer type and compare it to 2 in the right side of == operator.

Whereas in the second example it just SKIPS THE COERCION. This is important to understand that the engine is simply skipping the coercion step, not comparing the data type.

So, the result is the same but the reason is different!

Hope you felt enlightened as I felt when I found this out!! Cheers!