To append a string to another string in JavaScript, you can use the concat
method or the +=
operator to concatenate the strings.
In this example,
str1
with a value of "Hello"
.concat
method to append the string " World"
to str1
.concat
method concatenates the two strings and returns the result.let str1 = "Hello";
str1 = str1.concat(" World");
console.log(str1);
Hello World
In this example,
str2
with a value of "banana"
.+=
operator to append the string "apple"
to str2
.+=
operator concatenates the two strings and assigns the result to the variable str2
.let str2 = "banana";
str2 += "apple";
console.log(str2);
bananaapple
In this tutorial, we learned How to Append a String to another String in JavaScript language with well detailed examples.