I need to add in a For Loop characters to an empty string. I know that you can use the function concat in Javascript to do concats with strings
var first_name = "peter";
var last_name = "jones";
var name=first_name.concat(last_name)
but with my example it doesn't work. Any idea how to do it another way ?
my code :
var text ="";
for (var member in list) {
text.concat(list[member]);
}
This question is related to
javascript
string
for-loop
To use String.concat, you need to replace your existing text, since the function does not act by reference.
var text ="";
for (var member in list) {
text = text.concat(list[member]);
}
Of course, the join() or += suggestions offered by others will work fine as well.
You can also keep adding strings to an existing string like so:
var myString = "Hello ";
myString += "World";
myString += "!";
the result would be -> Hello World!
Simple use text = text + string2
simply used the +
operator. Javascript concats strings with +
It sounds like you want to use join
, e.g.:
var text = list.join();
Source: Stackoverflow.com