Facebook Pixel

Type Conversion & Coercion

JavaScript is a dynamically typed language, which means you don’t have to declare the type of a variable when creating it. The type of the value can change automatically — and this flexibility is both powerful and confusing for beginners.

In this tutorial, we’ll understand how JavaScript converts data types, what’s the difference between explicit and implicit conversion, and how to avoid common pitfalls.

What is Type Conversion?

Type conversion means changing the data type of a value from one type to another.

For example:

  • Converting a string to a number.
  • Converting a number to a string.
  • Converting a value to a boolean.

JavaScript does this in two ways:

  1. Explicit Conversion — You convert the type manually.
  2. Implicit Conversion (Type Coercion) — JavaScript converts it automatically behind the scenes.

1. Explicit Type Conversion (Manual Conversion)

In explicit conversion, you decide when and how to convert data types using built-in functions or methods.

Converting to String

You can convert numbers, booleans, or other types into strings using:

String(123);        // "123"
String(true);       // "true"
String(null);       // "null"
(123).toString();   // "123"

Converting to Number

To convert other types into numbers, use Number():

Number("123");    // 123
Number("3.14");   // 3.14
Number(true);     // 1
Number(false);    // 0
Number("hello");  // NaN (Not a Number)

You can also use:

  • parseInt("123abc")123
  • parseFloat("3.14xyz")3.14

Converting to Boolean

Use Boolean() to explicitly convert a value into true or false:

Boolean(1);      // true
Boolean(0);      // false
Boolean("Hi");   // true
Boolean("");     // false
Boolean(null);   // false
Boolean(undefined); // false

Rule: Values like 0, "", null, undefined, and NaN are falsy. Everything else is truthy.

2. Implicit Type Conversion (Type Coercion)

Sometimes JavaScript automatically converts values for you when it expects a certain type. This is called type coercion — and it often happens during operations involving different data types.

Example 1: String Coercion

When you add a number to a string, JavaScript converts the number into a string:

let result = "The number is " + 5;
console.log(result); // "The number is 5"

Even if you mix numbers and strings:

console.log(1 + "2");   // "12"
console.log("3" + 4 + 5); // "345"
console.log(3 + 4 + "5"); // "75"

Explanation: When the + operator sees a string, it assumes string concatenation, not addition.

Example 2: Numeric Coercion

When using operators like -, *, or /, JavaScript converts strings to numbers automatically:

console.log("10" - 5);  // 5
console.log("10" * 2);  // 20
console.log("10" / 2);  // 5

But beware:

console.log("10" + 5); // "105"  → String concatenation, not addition!

Example 3: Boolean Coercion

JavaScript converts values to true or false in logical operations:

if ("hello") {
  console.log("This runs!"); // because "hello" is truthy
}
 
if (0) {
  console.log("This won’t run."); // because 0 is falsy
}

Comparison and Coercion

The == (loose equality) operator in JavaScript also performs type coercion. It tries to convert values before comparing them.

console.log(5 == "5");    // true (string "5" is coerced to number 5)
console.log(0 == false);  // true
console.log("" == false); // true

To avoid this confusion, always use === (strict equality) — it compares both value and type:

console.log(5 === "5");   // false
console.log(0 === false); // false

Best Practice: Always use === and !== for comparisons to avoid unexpected results.

Quick Recap

Conversion Type Description Example
Explicit You manually convert types Number("123"), String(100)
Implicit (Coercion) JS automatically converts types "5" * 2 → 10, "5" + 2 → "52"
Boolean Coercion Converts values to true or false Boolean("") → false
Loose vs Strict Comparison == allows coercion, === doesn’t 5 == "5" → true, 5 === "5" → false