One of the most surprising things about JavaScript is how it sometimes seems to “change” data types without you asking. You might think you’re adding two numbers, but suddenly you get a string. Or you compare a number to a string, and JavaScript says they’re equal when they look different.

This behavior comes from type coercion and type conversion.


Type Conversion (Explicit Conversion)

This happens when you deliberately change one data type into another. It’s like saying, “JavaScript, I know what I want—make this a number, make this a string, or make this a boolean.”

Examples:

Number("42");     // 42
String(42);       // "42"
Boolean(1);       // true
Boolean(0);       // false

Here you’re in charge—you’re telling JavaScript exactly how to transform the value.


Type Coercion (Implicit Conversion)

This is when JavaScript does the conversion for you, often behind the scenes. Sometimes it’s helpful, sometimes it’s confusing.

For example:

1 + "2"   // "12"  (number coerced into a string)
1 - "2"   // -1    (string coerced into a number)

Notice how + caused concatenation when a string was present, while - forced numeric subtraction. That’s coercion in action!


Truthy and Falsy Values

Another famous part of coercion: when JavaScript needs a boolean, it will coerce almost anything into true or false.

  • Falsy values: 0, "" (empty string), null, undefined, NaN, and false.
  • Everything else is truthy.

Example:

if ("hello") {
  console.log("This runs!"); // because "hello" is truthy
}

if (0) {
  console.log("This will NOT run"); // 0 is falsy
}

Double Equals (==) vs Triple Equals (===)

The == operator allows coercion, while === doesn’t.

0 == "0";   // true  (coerced)
0 === "0";  // false (different types)

Best practice? Use === (strict equality) almost always, unless you really want coercion.


Quick Summary

  • Conversion = explicit, you control it (Number("42")).
  • Coercion = implicit, JavaScript decides (1 + "2").
  • Know your truthy and falsy values.
  • Prefer === over == for safer comparisons.

JavaScript’s coercion is like a magician—it can be impressive, but sometimes it pulls tricks you didn’t expect. Knowing how it works makes you the one in control.


Review Fill-in-the-Gap Questions

  1. Converting types on purpose is called __________ conversion.
  2. When JavaScript automatically changes a type behind the scenes, it is called __________.
  3. The expression 1 + "2" results in __________.
  4. The expression 1 - "2" results in __________.
  5. A string with content (like "hello") is considered __________ in a boolean context.
  6. The values 0, "", null, undefined, NaN, and false are all __________ values.
  7. 0 == "0" evaluates to __________ because of type coercion.
  8. To avoid unexpected conversions, it’s recommended to use __________ instead of ==.
  9. The function __________ can explicitly turn "42" into the number 42.
  10. Boolean("") returns __________ because an empty string is falsy.

<
Previous Post
The Illusion of Freedom: The Lessons from AI and the Divine
>
Next Post
JavaScript Destructuring: Breaking Things Apart Easily