Type Conversion

How JavaScript converts between types — sometimes helpfully, sometimes surprisingly.

Overview

Two kinds of type conversion in JavaScript.

JavaScript is a dynamically typed language. When an operation expects a certain type, JavaScript will try to convert values automatically. This is called type coercion.

You can also convert types manually using built-in functions. This is called explicit conversion.

✋ Explicit Conversion

You do it intentionally

Number("42") → 42

⚡ Implicit Coercion

JavaScript does it automatically

"5" * 2 → 10
// Three target types:
String(42)      // "42"     → to string
Number("42")    // 42       → to number
Boolean(1)      // true     → to boolean

Converting to String

Using String() and .toString() methods.

ValueString()Result
42String(42)"42"
trueString(true)"true"
falseString(false)"false"
nullString(null)"null"
undefinedString(undefined)"undefined"
NaNString(NaN)"NaN"
// Methods to convert to string:
String(42)          // "42" — explicit
(42).toString()     // "42" — method
42 + ""             // "42" — implicit (concatenation)

// Template literals always produce strings:
`Value: ${42}`      // "Value: 42"

Converting to Number

Using Number(), parseInt(), parseFloat(), and the + operator.

ValueNumber()Notes
"42"42Numeric string → number
" 42 "42Whitespace trimmed
""0Empty string → 0
"hello"NaNNon-numeric → NaN
true1Boolean conversion
false0Boolean conversion
null0null → 0 (special case)
undefinedNaNundefined → NaN

Try it — Number() converter

Number()
42
// Other conversion methods:
parseInt("42px")      // 42 (stops at non-numeric)
parseFloat("3.14abc") // 3.14
parseInt("0xFF", 16)  // 255 (hex)
+"42"                 // 42 (unary + operator)

Converting to Boolean

The simplest conversion — every value is truthy or falsy.

❌ Falsy (→ false)

  • falsethe boolean
  • 0, -0zero
  • ""empty string
  • nullno value
  • undefinednot assigned
  • NaNnot a number

✓ Truthy (→ true)

  • "hello"non-empty string
  • 42, -1non-zero number
  • "0"string zero (!)
  • "false"string false (!)
  • [], {}empty objects (!)
  • function(){}any function
// Explicit conversion:
Boolean(1)         // true
Boolean(0)         // false
Boolean("hello")   // true
Boolean("")        // false

// Double NOT shortcut:
!!1                // true
!!0                // false
!!"hello"          // true
!!""               // false

💡 Surprising truthys: "0", "false", [], and {} are all truthy! Only the 6 values listed above are falsy.

Implicit Coercion

When JavaScript converts types automatically.

// String concatenation with +
"5" + 3
// → "53" (number becomes string)

When + has a string operand, JavaScript converts the other operand to a string and concatenates.

💡 "5" + 3 = "53"
1 / 5

Gotchas & Quirks

The weird parts of JavaScript type conversion.

ExpressionResultWhy
[] + []""Arrays → strings → concat
[] + {}"[object Object]"String concat
"" == falsetrueBoth become 0
"0" == falsetrueBoth become 0
"0" == ""falseString comparison
null == 0falsenull only == undefined
NaN == NaNfalseNaN ≠ anything
// How to avoid coercion bugs:

// 1. Always use === instead of ==
5 === "5"   // false (safe!)

// 2. Convert explicitly
Number("42") + 8  // 50 (clear intent)

// 3. Check for NaN
Number.isNaN(NaN)  // true
isNaN("hello")     // true (converts first — less safe)

// 4. Use parseInt for user input
parseInt(prompt("Enter age:"), 10)

🏆 Rule of thumb:Be explicit. Convert types yourself rather than relying on JavaScript's implicit coercion.

Frequently Asked Questions

Common questions about type conversion.