JavaScript Type Coercion: Implicit vs Explicit Conversion

JavaScript can automatically convert values from one type to another, and it can also convert them when you ask it to. This article explains both implicit and explicit type coercion so you can predict results, avoid confusing bugs, and write clearer code.

Quick answer: Type coercion is the process of converting a value from one type to another. Implicit coercion happens automatically during operations like + or ==, while explicit coercion happens when you call functions such as String(), Number(), or Boolean().

Difficulty: Beginner

You'll understand this better if you know: basic JavaScript values such as strings, numbers, booleans, and how operators work.

1. What Is Type Coercion?

Type coercion is JavaScript's way of turning one value type into another. It matters because JavaScript tries to be flexible, but that flexibility can make expressions behave differently than beginners expect.

In practice, coercion is one of the reasons JavaScript expressions sometimes look simple but produce surprising results.

2. Why Type Coercion Matters

Coercion is important because many JavaScript operators and built-in APIs expect a certain type. If the value is not already that type, JavaScript may convert it for you.

You need to understand coercion when you: