-5

What will be an output of:

var number = "1.2";
console.log(number - 0.2);
console.log(number + 0.2);

And why?

JSON Derulo
  • 6,229
  • 3
  • 30
  • 39
Tom Smykowski
  • 24,566
  • 52
  • 155
  • 227

2 Answers2

0

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

CharybdeBE
  • 1,619
  • 22
  • 31
0

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

Abhishek Anand
  • 415
  • 4
  • 20