"123" is a string type but the value is an integer. So JavaScript convert it to an integer on compile time and allowed multiplication,Addition,etc. But "1-2" is a string type but why it does not allow to multiplication? And why JavaScript does not handle this?
console.log("1" * "123")//123
console.log(1 * "123")//123
console.log("1" * 123)//123
console.log("ABC" * 123)//NaN
console.log("ABC" * "ABC")//NaN
console.log("0" * "1-2")//NAN
console.log("0" * "1*2")//NAN
console.log("0" * "1+2")//NAN
console.log("0" * "1/2")//NAN
console.log("0" * (1-2))//0
EDIT:
-
is notsubtraction
but ahyphen
. OK. But it should be handle ashyphen
while concatenation not calculation.
Why JavaScript can't subtract/Add "1-1" while compiling. But JavaScript convert "123" as a integer on compile time, if the code is doing calculation. Then why it's doesn't calculate "1-1" or "1+1" to
0
or2
and convert into an integer?