Type Coercion — JavaScript

JavaScript sometimes quietly changes a value's type (type coercion). + with text joins ("5" + 3 gives "53"). -, * and / turn text into numbers ("5" - 3 gives…

In the Data Types lesson, + joined a price stored as text instead of adding it. Now look at this one: "5" - 3 gives 2, a number. Here "5" is text too, only the sign is -, not +, and this time JavaScript does the maths. Why?

Because JavaScript sometimes quietly changes a value's type so that an operation can run, for example it turns text into a number. This is called type coercion. A type is the kind of value: a number like 5, a string (text) like "5", or a boolean, which is true or false. If you don't know the rules, the results can surprise you.

The + sign loves text

These four lines use + and - on text, numbers and booleans. The comment on each line shows the result:

console.log("5" + 3);            // 53
console.log(5 + 3);              // 8
console.log("5" - 3);            // 2
console.log(true + true + true); // 3

All of these type changes happen automatically. If you want to change a type yourself, use Number() (to a number) or String() (to text). Number("5") + 3 is 8, and String(5) is "5".

Other options for turning text into a number:

Way When to use it
Number(text) The one this lesson uses, because the whole text must be a number: Number("12px") is NaN (not a number). Writing +text gives the same result.
parseInt(text) Takes the whole number at the start of the text: parseInt("12px") is 12.
parseFloat(text) The same, but it also reads decimals: parseFloat("1.5kg") is 1.5.

== converts types, === doesn't

There are two signs for checking whether two values are equal, == and ===. Compare their results:

console.log("5" == 5);    // true
console.log("5" === 5);   // false
console.log(5 === 5);     // true
console.log("five" == 5); // false

If a === b is true, then a == b is always true too. The opposite is not always so: a == b can be true while a === b is false, as with "5" and 5. In everyday code use ===, so that no type is changed without you noticing.

Truthy and falsy

This example shows values that are not booleans turning into true or false:

console.log(Boolean("cat")); // true
console.log(Boolean(0));        // false
console.log(Boolean(""));       // false
console.log(!!"cat");        // true

if ("cat") {
  console.log("runs"); // runs
}

if needs true or false, so any other value inside its parentheses is converted first. Values that turn into false are called falsy. The list is short: false, 0, "" (empty text), null, undefined and NaN (plus -0 and 0n, two rarely used forms of zero). Every other value is called truthy: it turns into true. That includes "0", "false", [] (an empty list) and {} (an empty object).

Boolean(value) turns any value into true or false, following the falsy list above. !!value gives the same result: one ! flips a value (truthy becomes false, falsy becomes true), and !! flips it twice.

⚠️ When JavaScript quietly guesses wrong

TL;DR: JavaScript sometimes quietly changes a value's type (type coercion). + with text joins ("5" + 3 gives "53"). -, * and / turn text into numbers ("5" - 3 gives 2), and true counts as 1. == converts types before it compares, while === compares value and type as they are, so use ===. The falsy values (they count as false inside an if) are false, 0, "", null, undefined and NaN. Every other value is truthy (it counts as true). Boolean(value) or !!value turns a value into true or false.

⚡ + joins, - does maths

Write:

console.log("5" + 3); // 53
console.log("5" - 3); // ?
Guess first: what does "5" - 3 give?

2, a number. The - sign can only do maths, so the text "5" is turned into the number 5.

Meaning: JavaScript sometimes changes a value's type automatically (type coercion). When either side of + is text, + joins, so "5" + 3 gives the text "53". -, * and / turn text into numbers. To change a type yourself, use Number(): Number("5") + 3 gives 8.

⚡ == converts types, === doesn't

Write:

console.log("5" == 5);  // true
console.log("5" === 5); // false

Meaning: == converts the types first, then compares: the text "5" becomes the number 5, so the result is true. === compares value and type without converting anything, so text and a number are never equal. In everyday code, use ===.

⚡ Truthy and falsy

Guess first: does if ("0") run or not?

It runs. "0" is text that isn't empty, so it is truthy: inside an if, it counts as true.

Meaning: if first turns the value inside its parentheses into true or false. The values that become false are called falsy, and they are only false, 0, "" (empty text), null, undefined and NaN. Every other value is called truthy and becomes true, including "false" and [] (an empty list).

What you will learn