JavaScript Course Lesson 9 of 16
The Codex / JavaScript / Type Coercion & Casting

Type Coercion & Casting

How JavaScript converts between types automatically — and how to do it deliberately with Number(), String(), and Boolean().

What it is, in plain English: Type coercion is JavaScript converting a value from one type to another automatically, without you asking. It happens during arithmetic, comparisons, and template literals. Sometimes it's helpful ('1' - 1 = 0). Often it's surprising ('1' + 1 = '11'). The fix: convert types explicitly yourself using Number(), String(), or Boolean() so JavaScript does exactly what you intended.

When JavaScript converts types — and how to control it

JavaScript sometimes converts a value from one type to another automatically — without you asking. This is called type coercion, and it's one of the main places where JavaScript surprises people. Once you understand the rules, you can avoid the surprises.

The biggest trap: + with strings

The + operator does two things in JavaScript:

  • If both sides are numbers → adds them
  • If either side is a string → concatenates (joins) them as text
// Numbers: + adds
console.log(1 + 2);         // 3

// String involved: + concatenates
console.log("1" + 2);       // "12"  ← "1" is a string, so 2 becomes "2"
console.log(1 + "2");       // "12"  ← same
console.log("Total: " + 99);// "Total: 99"  ← useful!

// Order matters with mixed types:
console.log(1 + 2 + "3");   // "33"  — 1+2=3 (numbers), then 3+"3"="33"
console.log("1" + 2 + 3);   // "123" — "1"+2="12", then "12"+3="123"
This is why user input always needs converting. When a user types a number into a form, it arrives as a string. Adding two form values with + joins them as text. Always convert with Number() first.
// Form input example:
const price = "9.99";   // came from user input — it's a string!
const qty = "3";

console.log(price * qty);          // 29.97 — * converts both to numbers ✓
console.log(price + qty);          // "9.993" — + concatenated! ✗

// Safe approach: convert first
console.log(Number(price) + Number(qty));  // 12.99  ✓ (9.99 + 3)

Other operators convert to numbers

Subtraction, multiplication, division, and comparisons all convert strings to numbers automatically. Only + concatenates.

console.log("10" - 5);    // 5   — "10" → 10
console.log("10" * 2);    // 20  — "10" → 10
console.log("10" / 4);    // 2.5 — "10" → 10
console.log("5" > 3);     // true — "5" → 5
console.log("abc" - 1);   // NaN — "abc" can't become a number

Explicit conversion — do it yourself, safely

Instead of relying on JavaScript's automatic conversions, convert deliberately:

// → Number
Number("42")        // 42
Number("3.14")      // 3.14
Number("")          // 0     ← careful
Number("abc")       // NaN
Number(true)        // 1
Number(false)       // 0
Number(null)        // 0     ← careful
Number(undefined)   // NaN

// parseInt — extracts the integer part, stops at first non-numeric char:
parseInt("42px")    // 42
parseInt("3.9")     // 3   — integer only
parseInt("abc")     // NaN

// parseFloat — extracts decimal number:
parseFloat("3.14em")  // 3.14

// → String
String(42)          // "42"
String(true)        // "true"
String(null)        // "null"
String(undefined)   // "undefined"
(42).toString()     // "42" — method on numbers

// → Boolean
Boolean(1)          // true
Boolean(0)          // false
Boolean("hello")    // true
Boolean("")         // false
Boolean(null)       // false
Boolean(undefined)  // false

The unary + operator — quick string to number

Putting + before a value converts it to a number — a common shorthand:

+"42"         // 42
+"3.14"       // 3.14
+true         // 1
+false        // 0
+null         // 0
+""           // 0
+"abc"        // NaN
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.