5
By declaring more than one variable of the same type on a line, I can subsequently perform arithmetic operations with these values. Ex.:
let num1 = 10, num2 = 5, num3= 10;
let resultado = num1 + num2 + num3;
console.log(resultado); //O resultado será 25
However, if I change the type of the first variable num1
to string and try to perform the same operation:
let num1 = "10", num2 = 5, num3 = 10;
let resultado = num1 + num2 + num3;
console.log(resultado); //O resultado será 10510
Why does this behavior occur? Why are all variables converted?
First remark: When a string variable starts,this happens,now if it is after no and some if it gives Nan.
– Maury Developer
https://answall.com/questions/126163/por-que-a-minha-fun%C3%A7%C3%A3o-est%C3%A1-concatenating-ao-Inv%C3%A9s-de-summing-os-n%C3%Bameros
– André
I can give you an example:
"2"+2="22"; 2+2+"2"="42"
– Maury Developer
I think it is worth mentioning that the fact that the variables are declared on the same line makes no difference, what matters is only the type of them.
– hkotsubo