const num1 = 10;
const num2 = "10";
console.log(num1 * num2); // Output: 100
console.log(num2 * num2); // Output: 100
Asked
Active
Viewed 114 times
-3

Barmar
- 741,623
- 53
- 500
- 612
-
3JavaScript performs automatic type conversion. You can view this as a convenience or a curse. – Barmar Apr 04 '22 at 21:04
1 Answers
1
Javascript has a feature called type coercion that allows values of one type to be implicitly converted into a type that is acceptable for an operation.
In "10" * 10
, JS knows that multiplication with a string and number does not make sense, so it automatically converts the string into a number.
(However, in "10" + 10
, JS sees +
as a string concatenation operation and produces "1010"
.)

Ethan McTague
- 2,236
- 3
- 21
- 53