Why ss*2
is not 4545
?
Great question! *
is not an operation that is available for strings. (There is a .repeat()
method: String.prototype.repeat() - JavaScript | MDN)
Because JavaScript is not explicitly typed, when it sees a numeric operation, it will attempt to coerce the value into a number.
2 Likes
Because JavaScript is not explicitly typed, when it sees a numeric operation, it will attempt to coerce the value into a number.
This is correct, but worth noting is if one had tried ss+2
, the operation wouldn’t be 45+2
because the + gets prioritized as a concatenation operation of the string ‘45’.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Addition#string_concatenation
2 Likes