Two things on Bayu’s counter and one operator: see every new slip +, -, < or == writes before it acts, including the valueOf and toString calls the engine…
Why it matters
Coercion is a classic source of JavaScript bugs: a form field joined instead of added, an array turned into text, null turned into 0. Know the rules and results that look random become predictable, and it is clear why === and deliberate conversion are safer.
The key idea
+ leans to text, the other operators lean to numbers
Common mistakes
+ with text turning into accidental joiningtotal + field can become the text '510'. Convert on purpose first: total + Number(field).
Using == and hitting a rule you did not expect[] == false is true, '' == 0 is true too. Use === so nothing is converted at all.
Assuming null == 0 is true False! null and undefined are only == to each other. But null + 1 is 1, because + uses Number(null), which is 0.
Try it in this playground
Join a number to text with + '100' + 50: one side is text, so + joins.
Take a number from text - has no text mode: the result is a number.
Watch an array go through valueOf, then toString valueOf gives the array itself, so toString is used.
Call Symbol.toPrimitive The uang object hears the operator’s hint.