Concatenating string with number in Javascript
Posted
by
Sparky
on Stack Overflow
See other posts from Stack Overflow
or by Sparky
Published on 2011-01-11T13:46:31Z
Indexed on
2011/01/11
13:53 UTC
Read the original article
Hit count: 172
JavaScript
|string-concatenation
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?
© Stack Overflow or respective owner