What is type coercion?
Type coercion is the process of converting a value from one type to another type. (such as string to number).
It is something like this one equals to string one with double equals.
console.log(1 == "1");
true
That is number and string type one of them will be converted into an equivalent value by the JavaScript engine.
Do all languages have type coercion?
Yes, they do because we always need to covert types between programs to do things. In memory, different types look completely different than what we type. The number 8 in actual physical memory on the computer is represented in 1 to 0. Thus, all languages do this. It just so happens that JavaScript has an especially heavy type coercion in nature to it because it is dynamically type.
In JavaScript type coercion does not happen just with the quals sign
if (1) {
console.log("one is true")
}
console.log(-0 === +0);
console.log(Object.is(-0, +0));
console.log(NaN === NaN);
console.log(Object.is(NaN, NaN));
one is true
true
false
false
true
- JavaScript Type coercion
one
equaltrue
.