[javascript] Add characters to a string in Javascript

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

The answer is


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();

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to for-loop

List append() in for loop Prime numbers between 1 to 100 in C Programming Language Get current index from foreach loop how to loop through each row of dataFrame in pyspark TypeScript for ... of with index / key? Is there a way in Pandas to use previous row value in dataframe.apply when previous value is also calculated in the apply? Python for and if on one line R for loop skip to next iteration ifelse How to append rows in a pandas dataframe in a for loop? What is the difference between ( for... in ) and ( for... of ) statements?