TExcelTAD
Home › JavaScript › JS Type Conversion

JavaScript Type Conversion

Values often need to change type: text from a form field must become a number, a number must become a string for display, any value must become a boolean in an if. JavaScript converts types in two ways:

  • Explicit conversion — you call Number(), String() or Boolean() yourself.
  • Implicit conversion (coercion) — an operator converts values automatically, e.g. "5" * 2.

Explicit conversion makes your intent obvious; implicit conversion is convenient but is the source of many famous JavaScript surprises.

Converting to a number

js · try it yourself
console.log(Number("42"), Number("  3.5  "), Number(""));
console.log(Number("12px"), Number("abc"));
console.log(Number(true), Number(false));
console.log(Number(null), Number(undefined));
console.log(Number([]), Number([7]), Number([1, 2]));
console.log(parseInt("12px", 10), parseFloat("3.5kg"));
console.log(+"8" + 2);   // unary plus converts too
42 3.5 0
NaN NaN
1 0
0 NaN
0 7 NaN
12 3.5
10
Value Number(value)
"42", " 42 " 42 (white space is trimmed)
"", " " 0
"42px", "abc" NaN
true / false 1 / 0
null 0
undefined NaN

Converting to a string

String(value) works for every value, including null and undefined. value.toString() does the same but throws on null/undefined:

js · try it yourself
console.log(String(123), String(true), String(null), String(undefined));
console.log(String([1, 2, 3]), String({}));
console.log((255).toString(16), (3.14159).toFixed(2));
console.log(`${42}` + "!", 7 + "");
try {
  null.toString();
} catch (err) {
  console.log(err.message);
}
123 true null undefined
1,2,3 [object Object]
ff 3.14
42! 7
Cannot read properties of null (reading 'toString')

Converting to a boolean

Boolean(value) (or !!value) follows the truthy/falsy rules: only false, 0, -0, 0n, "", null, undefined and NaN become false.

js · try it yourself
console.log(Boolean(""), Boolean("0"), Boolean(" "));
console.log(Boolean(0), Boolean(-1), !!NaN);
console.log(Boolean([]), Boolean({}), !!null);
false true true
false true false
true true false

Implicit conversion (coercion)

Operators convert their operands according to fixed rules:

  • + with any string operand → string concatenation.
  • -, *, /, %, ** → always numeric.
  • == → converts to a common type (see Comparisons).
  • if, &&, ||, ! → boolean context.
js · try it yourself
console.log("3" + 4, 3 + "4", 3 + 4 + "5", "3" + 4 + 5);
console.log("10" - 2, "10" * "2", "10" / "4", "five" * 2);
console.log(true + 1, null + 1, undefined + 1);
console.log([] + [], [] + {}, [1, 2] + [3]);
34 34 75 345
8 20 2.5 NaN
2 1 NaN
 [object Object] 1,23

The last line shows why coercion has a bad reputation: arrays and objects first become strings ([] → "", {} → "[object Object]", [1, 2] → "1,2") and are then concatenated.

A safe conversion pattern

When converting user input, convert explicitly and check the result:

js · try it yourself
function toQuantity(input) {
  const n = Number(String(input).trim());
  if (input === "" || !Number.isInteger(n) || n < 0) {
    return { ok: false, error: `"${input}" is not a valid quantity` };
  }
  return { ok: true, value: n };
}

for (const raw of ["3", " 12 ", "", "2.5", "-1", "ten"]) {
  console.log(toQuantity(raw));
}
{ ok: true, value: 3 }
{ ok: true, value: 12 }
{ ok: false, error: '"" is not a valid quantity' }
{ ok: false, error: '"2.5" is not a valid quantity' }
{ ok: false, error: '"-1" is not a valid quantity' }
{ ok: false, error: '"ten" is not a valid quantity' }

Dates

A Date converts to its timestamp in numeric contexts, which makes date arithmetic work:

js · try it yourself
const d = new Date("2025-01-01T00:00:00Z");
console.log(Number(d), +d === d.getTime());
console.log(new Date("2025-01-02T00:00:00Z") - d, "ms in a day");
1735689600000 true
86400000 ms in a day

Tip: Prefer ===, convert input explicitly with Number() or String(), and never rely on + to add values that might be strings. Tools like TypeScript catch most accidental coercion before your code ever runs.

Test yourself

What is "6" - "2"?

  1. "62"
  2. "4"
  3. 4
  4. NaN

What is Number(null)?

  1. NaN
  2. 0
  3. null
  4. undefined

What is 1 + 2 + "3"?

  1. "123"
  2. "33"
  3. 6
  4. "6"