Reputation: 8962
in php, i do this to connect strings
(pseudo code)
$myString = "";
for($i = 0;$i < 10;$i++)
$myString .= $i;
echo $myString;
would give me 0123456789
you got the idea ,now, how can i do the same in javascript?
Upvotes: 0
Views: 614
Reputation: 14222
Javascript uses the plus sign for string concatenation. So:
mystring = 'this' + 'that'; //gives string value "thisthat"
It is important to note that Javascript also uses the plus sign for numeric addition. This means that you can get into trouble with variable types.
var myInt = 5;
var myString = "5";
alert(myInt + 5); //gives the string value "55".
alert(myString + 5); //gives the integer value 10.
This means that your PHP trick of adding numbers together to make string, as per your question, will only work if you start with a string variable. PHP can recognise for itself that you intend it to be a string operation because of the concat operator; Javascript doesn't have that ability, so you have to tell it explicity by making sure that your variables are of the correct type.
Upvotes: 2
Reputation: 33993
You should use the += operator.
So in pseudo code your code should be something like
myString = "";
for(i = 0;i < 10;i++)
myString += i;
alert(myString);
Upvotes: 3
Reputation: 7345
Here it will do it.
var $myString = "";
for(var $i = 0;$i < 10;$i++){
$myString += $i;//+= the equivalent of .= in JS
}
alert( $myString );
Upvotes: 1
Reputation: 6147
var myString = "";
for(var i = 0;i < 10;i++)
myString += i;
alert(myString);
Upvotes: 3