Implicit Type Casting
Implicit conversion follows the operator involved. + can concatenate strings, while arithmetic subtraction converts operands to numeric values.
Read the operator and the input types before predicting the result.
See the mechanism
"42"Follow one example
The same input participates in different operations. + produces "42", - produces 2, and loose equality applies its own conversion rules to yield true for "" == 0. Do not extrapolate one operator’s behavior to every other operator. Normalize expected input before applying business rules.
Predict, change and check
Predict: What would replacing input with false do to the addition and subtraction?
Test your explanation: Test false, then null, recording both the operation and the result. Replace the loose comparison with === to separate conversion from comparison.
The tasks below ask you to explain that result and apply the idea to different inputs.
Apply and distinguish
Coercion belongs to the operation
For primitive operands, subtraction attempts numeric conversion, while + may concatenate when a primitive string is involved. A form value that participates in subtraction can appear to work and then fail when the code switches to addition. That inconsistency is evidence of an unreviewed boundary, not of random arithmetic.
Array and object conversion can produce surprising primitive strings. Instead of memorizing isolated puzzles, inspect operand types, identify the conversion rule for the operation, and test the exact input shape. Prefer a clear normalization function over scattering coercion-dependent expressions throughout rendering code.
Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.