Implicit conversion
The -, * and / operators work only with numbers, while + can also join strings. That is why + works with a string and a number differently from -, * and /:
console.log("4" + 2); // 42
console.log("4" - 2); // 2
console.log("4" * "2"); // 8
console.log(typeof ("4" + 2)); // stringLet's walk through the program line by line:
"4" + 2prints42: the two was glued onto the string."4" - 2prints2: the minus turned the string into a number and subtracted."4" * "2"prints8: multiplication turned both strings into numbers.typeof ("4" + 2)printsstring: the result of the addition is a string.
This program has neither Number nor String, yet the types still change. A change of type that JavaScript makes on its own is called implicit conversion. The == operator from the lesson on comparison does it too when the sides have different types: that cashier also changed the currency before comparing.
Implicit conversion is like an exchange office that changes your money without asking. You asked for nothing, yet the value already has another type. That is why such places in code often go unnoticed until a wrong result shows up.
Plus and strings
If at least one side of + is a string, the other side becomes a string too, and the strings are joined. Joining strings is called concatenation.
The expression is evaluated left to right, so the order matters:
- in
1 + 2 + "3", first1 + 2gives the number3, and the result is"33"; "1" + 2 + 3joins strings from the first step and gives"123".
Compound assignment += joins as well when the variable holds a string. This is how a string is built piece by piece, starting from an empty one: "" + 1 is the string "1".
let out = "";
out += 1;
out += 2;
console.log(out, typeof out); // 12 string
console.log(1 + 2 + "3", "1" + 2 + 3); // 33 123Step by step:
outstarts as an empty string.out += 1gives the string"1", andout += 2glues a two onto it:"12".- The program prints
12 string. - The last line prints
33 123: the order of steps decides when the joining starts.
Interviewers ask this all the time: what do you get when you add a number to a string, and what when you subtract it?
The rest of arithmetic
The -, *, /, % and ** operators always turn both sides into numbers, the way Number does. A minus in front of a single string gives a number too: -"5" is -5. If a string does not look like a number, the result is NaN. From there NaN spreads: -, *, / and + with a number give NaN again.
Number.isNaN(x) helps check the result: it returns true only if x is NaN. A comparison with === will not help here, since NaN is not equal even to itself.
Choosing between two values
The conditional operator condition ? a : b helps choose one of two values by the result of a check such as Number.isNaN. It reads well as a question: "is the condition true? then a, otherwise b".
How it works:
- the condition can be any value: it is checked as truthy or falsy, by the same rules as for
&&and||; - if the condition is truthy, the operator returns
a, and otherwiseb; - it is the only JavaScript operator that takes three values.
console.log("10" / "4", "a" * 2); // 2.5 NaN
const total = "a" * 2 + 1;
console.log(total, Number.isNaN(total)); // NaN true
const safe = Number.isNaN(total) ? 0 : total;
console.log(safe); // 0Line by line:
"10" / "4"gives2.5, and"a" * 2givesNaN: the string"a"does not look like a number.totalgetsNaN, becauseNaN + 1isNaNagain.Number.isNaN(total)printstrue.- The condition of the conditional operator is truthy, so it returns
0, and the program prints0.
Common mistake
One is added to a number taken from a string, expecting the next number:
const input = "5";
const next = input + 1; // ✗
console.log(next); // 51The left side of + is a string, so the one was glued onto it, giving "51". Subtraction in the same program would work correctly: input - 1 gives 4, because - always turns a string into a number. Turn the string into a number first: Number(input) + 1 gives 6.
Key points
- If one side of
+is a string,+joins strings; the expression is evaluated left to right. -,*,/,%and**always turn both sides into numbers, and a failure givesNaN, which spreads further.Number.isNaN(x)checks thatxisNaN.condition ? a : breturnsawhen the condition is truthy andbwhen it is falsy.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.