What will be an output of:
var number = "1.2";
console.log(number - 0.2);
console.log(number + 0.2);
And why?
What will be an output of:
var number = "1.2";
console.log(number - 0.2);
console.log(number + 0.2);
And why?
The output is
1
1.20.2
Why ?
In the first case the variable string is converted to number because there is no -
operator for strings
But there is a +
operator for string and it make a concatenation of string, it is in the second case prefered to first convert into number
The answer will be 1 and 1.20.2 respectively
Note that number is string, but since - operator is not supported by string, JS converts it to number thus the output 1. for the second case, since + operator is supported by string, it will simply concatenate it, hence the answer 1.20.2