JavaScript » String Operator

The + plus string operator is used to concatenate (join together) two or more strings.

Examples

Code:
document.write("bread " + "and " + "cheese");
Output:
bread and cheese
Explanation:

This example joins together the three strings 'bread', 'and' and 'cheese' to produce 'bread and cheese'.


Code:
var x='honey';
document.write("milk and " + x);
Output:
milk and honey
Explanation:

assuming 'x' to contain the string 'honey', return 'milk and honey'.


The shorthand assignment operator can also be used to concatenate strings.

Code:
var x = 'milk and ';
x += "honey";
document.write(x);
Output:
milk and honey
Explanation:

If the variable 'x' has the value 'milk and ', then the above code will add the string 'honey' to the value in variable 'x' producing the new string 'milk and honey.