149

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 it doesn't work with my example. Any idea of how to do it in another way?

My code :

var text ="";
for (var member in list) {
  text.concat(list[member]);
}
1
  • 2
    If list is an array, then don't use for...in but a normal for loop. More information here: developer.mozilla.org/en/JavaScript/Reference/Statements/… Btw. if you look closely at both of your examples, you can see the difference ( name=first_name.concat(last_name) vs text.concat(list[member]) Commented Apr 22, 2011 at 11:16

9 Answers 9

198
let text = "";
for(let member in list) {
  text += list[member];
}
Sign up to request clarification or add additional context in comments.

1 Comment

A better way might be to get the keys from the object instead of iterating (for...in), and join on those: var text = ""; text += Object.keys(list).join('')
75

You can also keep adding strings to an existing string like so:

var myString = "Hello ";
myString += "World";
myString += "!";

the result would be -> Hello World!

Comments

12

simply used the + operator. Javascript concats strings with +

Comments

6

It sounds like you want to use join, e.g.:

var text = list.join();

1 Comment

Only works for arrays. But I'm afraid that the OP uses for...in to traverse an array, so your suggestion probably works.
6

To use String.concat, you need to replace your existing text, since the function does not act by reference.

let text = "";
for (const member in list) {
  text = text.concat(list[member]);
}

Of course, the join() or += suggestions offered by others will work fine as well.

1 Comment

This is a good solution because it leaves the door open for function chaining
3

Simple use text = text + string2

Comments

2

Try this. It adds the same char multiple times to a string

const addCharsToString = (string, char, howManyTimes) => {
  string + new Array(howManyTimes).fill(char).join('')
} 

Comments

1

You can also use string interpolation

let text = "";
for(let member in list) {
  text = `${text}${list[member]}`;
}

1 Comment

That does not look like what interpolation was meant to be used for.
1

your array of strings (list) could work with map and join; (Also possible to additionally change strings if you want)

var text = list.map(i => `${i}`).join(' ') 

would return First Name

but if you want to add more things around the name, above template also helps:

var text = list.map(i => `'${i}'`).join(' ') 

would return 'First' 'Name'

and in case you would want to have a sequence of names, separeted by comma

var text = list.map(i => `'${i}'`).join(',') 

would return 'First','Name','Second','Third',...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.