What is type coercion, and when does JavaScript do it implicitly?
Type coercion is the engine changing a value from one type to another on its own. JavaScript is a dynamically typed language, so it does this whenever an operator receives a type that it cannot work with directly. The simple rule is that coercion happens with the arithmetic operators and with the string operators, and it does not happen with the strict equality operator.
console.log('5' + 3); // '53' - one value is a string, so both become strings
console.log('5' - 3); // 2 - the minus sign only works on numbers
console.log('5' * '2'); // 10 - both values are converted to numbers
console.log(true + 1); // 2 - true becomes 1
console.log(null + 1); // 1 - null becomes 0
console.log([] + {}); // '[object Object]' - both become strings
The reason this surprises people is that the plus sign does two different jobs. The rules say that addition with a string is string joining, and that addition with two non-strings is number addition. Every other arithmetic operator only works on numbers, so it converts its two values first.
Objects are the harder case. Before an operator runs, the engine turns its values into simple values using the standard conversion steps. It looks for a valueOf method first, and then for a toString method. An array inherits a toString that joins its items with commas, which is why an empty array becomes an empty string, and why an array of numbers becomes a comma separated list.
Coercion is not a bug in the language so much as the result of a permissive design, and it is still the single biggest source of surprising JavaScript bugs. The practical advice is to convert values explicitly at the edges of your program, where data arrives from a form field, a query string, or an API response, and to trust the types everywhere else.