JS String Concatenation


concat() method joins strings:

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


Or, simplely use "+" operator:

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


Or, use "+=" operator:

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


Another way to join an array of strings:

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

endmemo.com © 2024  | Terms of Use | Privacy | Home