JS String Concatenation


concat() method joins strings:

var s1 = "perschon.com";
var s2 = "javascript";
var s3 = "tutorial";
var s = s1.concat(" ", s2, " ", s3);
//s is now "perschon.com javascript tutorial"


Or, simplely use "+" operator:

var s1 = "perschon.com";
var s2 = "javascript";
var s3 = "tutorial";
var s = s1 + " " + s2 + " " + s3; //same result


Or, use "+=" operator:

var s = "perschon.com";
var s2 = "javascript";
s += " " + s2; //s = "perschon.com javascript"
var s3 = "tutorial";
s += " " +s3; //s="perschon.com javascript tutorial"


Another way to join an array of strings:

var arr = ["perschon","js", "string"];
var s = arr.join(" "); //s = "perschon js string"

convert.idontcarewhatyouthink.net © 2021  Terms of Use | Home