Concatenating string with number in Javascript
- by Sparky
I'm trying to create a simple calculator in Javascript. I have an array named expression
chunk[0] = 12
chunk[1] = + (the "+" sign)
chunk[1] = 5
I used a for loop to loop through the chunks (chunk[]) and join then into a single expression as follows:-
equation = ""; // To make var equation a string
for(i = 0; i <= length; i++)
{
equation = equation + expression[i];
alert(expression[i]);
}
alert(equation);
alert(expression[i]) showed values 12, + and 5.
But alert(equation) showed 125 (instead of "12+5"). I need the variable equation to be "12+5" so that I can later call eval(equation) and get the value of 12+5.
What am I doing wrong here?