-1

I am practicing Javascript operators but addition is not working. What should I do to fix the problem?

This for a new website. I gave 10+20. but in output i get 1020 instead of 30.

var x="10";
var y="20";
var z=x+y;

I expect the output of 10+20 to be 1020, but the actual output is 30.

  • 1
    You're adding *strings* not numbers. You've got the values in quotes for some reason, so they're not numbers. – Pointy Nov 09 '19 at 13:06
  • 3
    Possible duplicate of [Adding two numbers concatenates them instead of calculating the sum](https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum) – Nick Parsons Nov 09 '19 at 13:18

1 Answers1

1

It's because you are trying to concatanate String instead. Using quote explicity tell to the intertreter yours variables are string. Discards them and you will get your addition.

Saver
  • 52
  • 5