-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
  • 9,780
  • 7
  • 39
  • 56
Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236

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,791
  • 1
  • 20
  • 35
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
  • 447
  • 5
  • 22