#4) Explain Implicit Type Coercion in JavaScript❔

myk

Mayank Yadav

Posted on July 25, 2021

#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.👆


💖 💪 🙅 🚩
myk
Mayank Yadav

Posted on July 25, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related