Adding two numbers concatenates them instead of calculating the sum
You need to use javaScript's parseInt()
method to turn the strings back into numbers. Right now they are strings so adding two strings concatenates them, which is why you're getting "12".
They are actually strings, not numbers. The easiest way to produce a number from a string is to prepend it with +
:
var x = +y + +z;
I just use Number()
:
var i=2;
var j=3;
var k = Number(i) + Number(j); // 5