Type Coercion

When the types do not match

Ask JavaScript to combine a string with a number and it will not stop and complain. It quietly converts one of them and carries on. That silent conversion is called type coercion, and it is one of the largest differences between JavaScript and the languages that refuse the mixture outright.
Coercion is occasionally convenient and frequently the reason a program prints something absurd. The rules are short, and once you know them the absurd output stops being a mystery.

The plus sign has two jobs

Every other arithmetic operator does exactly one thing. The + sign does two, and it chooses between them by looking at what sits on either side of it.
• If either side is a string, + joins the two into text, converting the other side to text first.
• Otherwise, + adds.
That is the entire rule. The word to notice is either. One string on one side is enough to switch the operator's job, no matter what is on the other side.

Everything else converts toward a number

Subtraction, multiplication, division and modulo have no second job to pick from. Handed text, they convert it into a number and get on with the arithmetic.
ExpressionResultWhy
"5" - 50minus converts the text, then subtracts
"6" * 212multiplication converts too
"6" / 23and so does division
"5" + 555plus saw a string, so it joined instead

One pair at a time, left to right

A line holding several + signs is not judged all at once. JavaScript works left to right, and each + decides its own job from whatever it is holding when its turn arrives. The result of one + becomes an operand of the next, so as soon as one + has produced text, every + after it on that line is stuck joining as well.

When a conversion has nothing to work with

Number() converts text into a value whenever the text spells one out. When it does not spell one out, JavaScript still refuses to raise an error. It hands back NaN, short for Not a Number.
The program below uses + with numbers and a string in different orders, and minus with a string. Run it, then change it and run it again:
• Put quotes around the 6, so that the first line reads let apples = "6";.
• Change "65" to "sixty".
• Press Reset, then change the minus sign in the last line to a plus sign.
let apples = 6;
let pears = 7;
let label = "Fruit: ";
console.log(apples + pears);
console.log(label + apples + pears);
console.log(apples + pears + " pieces");
console.log("65" - apples);
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

Two equality operators, and only one of them is safe

The == operator is allowed to coerce before it compares, and is called the loose operator for exactly that reason. The === operator is the strict one: it inspects the types first and reports false the moment they differ, without ever looking at the contents.
Those two names are the ones the rest of this course uses, so learn them here, where you can see what each operator is doing.
ExpressionResult
5 == "5"true
5 === "5"false
5 === 5true
"5" === "5"true

Where this bites in a real program

Everything prompt() hands back is text, however numeric the typed characters look. Add two of those together without casting and the + does exactly what its rule says.