concat()
joins two or more strings:
Example
var text1 = "Hello";
var text2 = "World";
var text3 = text1.concat(" ", text2);
var text2 = "World";
var text3 = text1.concat(" ", text2);
The concat()
method can be used instead of the plus operator. These two lines do the same:
Example
var text = "Hello" + " " + "World!";
var text = "Hello".concat(" ", "World!");
var text = "Hello".concat(" ", "World!");
All string methods return a new string. They don't modify the original string.
Formally said: Strings are immutable: Strings cannot be changed, only replaced.
Practice Excercise Practice now