Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Type Casting  ›  Lesson

Conversion vs Coercion

Type Conversion vs Coercion 12 minType conversion is a change of type you request explicitly, while coercion is a change the engine makes implicitly, and both always end in a string, number or boolean.

Conversion vs Coercion

In short: JavaScript changes a value's type constantly — sometimes because you asked, sometimes because it decided to. Type conversion (explicit casting) is a change you write yourself by calling a function named after the target type: Number(x), String(x), Boolean(x). Type coercion (implicit casting) is a change the engine makes on its own, because an operator or a statement needed a different type than you gave it. Every conversion, whichever way it happens, lands on one of exactly three targets: string, number, boolean.

Left panel, a hand pressing Number("214") turning it into 214 labelled explicit conversion; right panel, "214" * 1 quietly becoming 214 with a dashed hand behind a curtain, labelled implicit coercion, nothing names the change

Reading a form length on purpose

The clearest kind of type change: you name the type and call it.

javascript

Number(typed) is explicit conversion — the code says, right there, "turn this into a number." Nothing about the result is a surprise, because the change is written on the line.

The same change, hidden inside an operator

Now the same string meets an operator without anyone calling a conversion function:

javascript

"214" * 1 needs numbers on both sides, so the engine coerces the string to 214 — same result as Number(), but nothing in the code names it. "Plays: " + 3 coerces the number 3 into text instead, because + sees a string on one side. And if (...) always needs a boolean, so it coerces whatever you give it using the truthy/falsy rules. Three different targets, same silent mechanism.

Spotting both in one line

Real expressions often mix explicit and implicit changes in the same statement, and the hidden half decides the answer:

javascript

Number("214") is explicit — you can point at it. The division then has two numbers, so nothing changes there. But "Length: " + minutes coerces minutes into text, silently, because it meets a string at +. Read every expression left to right and ask at each operator: "was a type named here, or did two different types just collide?"

Spotting the hidden changeStep 1 of 4
  1. The line

    const label = "Length: " + minutes; with minutes equal to Number("214") / 60.

Quick practice

quick drill

Number("42") is an example of what?

quick drill

"42" * 1 is an example of what?

quick drill

In if ("hello") { ... }, what happens to the string?

quick drill

What does String(214) + 1 give?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFind both kinds

In const minutes = Number(input.value) / 60; const label = "Length: " + minutes;, point out every type change, say whether each is conversion or coercion, and explain how you can tell.