DEV Community

Cover image for #4) Explain Implicit Type Coercion in JavaScript❔
Mayank Yadav
Mayank Yadav

Posted on • Updated on

#4) Explain Implicit Type Coercion in JavaScript❔

💠The process of automatic or implicit conversion of values from one data type to another.

💠It takes place when the operands of an expression are of different data types.

String Coercion

✅It occurs when ' +, -, /, * ' operator is used.

✅When a number is added to a string, the number type is always converted into string type.

image

✅When a number is divided, subtracted or multiplied to a string, the string is always converted into number type.👇
image

Boolean Coercion

✅When a Boolean is added to a Number, the Boolean value is converted to a number.

✅A Boolean value can be represented as 0 for false or 1 for true.
image
⚠All values except 0, 0n, -0, "", undefined, null, NaN are truthy values.

Equality Coercion

✅The '==' operator compares values but not types.

✅Returns true because both 'a' and 'b' are converted to the same type and then compared. Hence the operands are equal.👇

image
✅Returns false as string 'true' is coerced to NaN which is not equal to 1 or true in Boolean, so returns false.👆


Top comments (0)