Implicit Coercion in Operations
Predict how + and comparisons coerce types.
Implicit Coercion in Operations is a free JavaScript Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Implicit Coercion
Implicit coercion happens when an operator converts operands to a type that the operation needs. Different operators trigger different conversions.
console.log("5" + 1)
console.log("5" - 1)Plus With Strings
The plus operator is special: if either operand is a string, it performs string concatenation instead of numeric addition.
console.log("3" + 4)
console.log(3 + "4")
console.log("a" + 1 + 2)Number Plus Number
When both operands are numbers, plus adds them. The order of operations matters when strings appear later in the chain.
console.log(1 + 2 + "3")
console.log("1" + 2 + 3)Minus Forces Numbers
The minus operator has no string meaning, so it always converts both operands to numbers, even strings that look numeric.
console.log("10" - "4")
console.log("10" - 4)Multiply and Divide
Like minus, the multiply and divide operators coerce operands to numbers. Non-numeric strings produce NaN.
console.log("6" * "2")
console.log("abc" * 2)Unary Plus
A unary plus in front of a value coerces it to a number, a compact conversion trick.
console.log(+"42")
console.log(+"3.14")
console.log(+true)Comparison Coercion
Relational operators like less-than convert operands to numbers, unless both are strings, in which case they compare alphabetically.
console.log("10" > 5)
console.log("apple" < "banana")Mixed Comparisons
When one side is a string and the other a number, the string is converted to a number for comparison.
console.log("20" > 3)
console.log("3" > 20)Boolean in Arithmetic
Booleans become 1 or 0 in arithmetic, so adding true to a number increments it.
console.log(true + true)
console.log(5 + false)Arrays in Coercion
Arrays convert to strings when used with plus. An empty array becomes an empty string, and a single-element array becomes that element as text.
console.log([] + [])
console.log([1, 2] + [3])Avoiding Surprises
To prevent accidental concatenation or NaN, convert explicitly with Number or String before operating. Clear intent beats relying on coercion.
const a = "5"
const b = "3"
console.log(Number(a) + Number(b))Quick Check
Test your coercion knowledge.
Recap
Recap: Operators trigger different coercions.
- Plus concatenates if any operand is a string
- Minus, multiply, divide force numbers
- Comparisons coerce to numbers unless both are strings
- Convert explicitly to avoid surprises
console.log("5" + 1)
console.log("5" - 1)Frequently asked questions
Is the “Implicit Coercion in Operations” lesson free?
Yes — the full text of “Implicit Coercion in Operations” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Implicit Coercion in Operations”?
Predict how + and comparisons coerce types. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Implicit Coercion in Operations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Truthy and Falsy Values
- Loose vs Strict Equality
- Implicit Coercion in Operations
- Explicit Conversion Techniques