JavaScript String Concatenation Tutorial

In this section, we will learn how to concatenate two string values and create a new one as a result.

JavaScript String concat() Method

The concat() method is used to concatenate multiple string values together in order to create one final string value as a result of this concatenation.

Note: using concat() method won’t change the original string values.

JavaScript String concat() Syntax

string.concat(string1, string2, ..., stringX)

String concat() Method Parameters:

The parameters of the concat() method is a number of string values that we want them to be concatenated and create a new string as a result.

Note: if the parameters are not of type string, JavaScript engine will try to convert them into a string value.

String concat() Method Return Value:

The return value from the concat() method is a new string that concatenated all the string values we set as the arguments for this method.

Example: string concatenation in JavaScript

const str = "John Doe";

const email = " [email protected] ";

const age = 29;

const res = str.concat(email, age);

console.log(res);

Output:

John Doe [email protected] 29

Example: JavaScript String Join

const str = "John Doe";

const email = " [email protected] ";

const bool = true;

const res = str.concat(email, bool);

console.log(res);

In this example, the value of the “bool” variable is not a string and so JavaScript force that into string (which is equal to the value true for this example) and then concatenates all the values together and produced one final string value.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies