In JavaScript, you can concatenate two strings, which means joining them together to create a single string.
There are multiple ways to achieve this.
Here are some ways to concatenate two strings:
1. Using the +
operator
The simplest way to concatenate two strings in JavaScript is to use the +
operator, which is also known as the string concatenation operator.
For example:
const str1 = "Hello";
const str2 = "world";
const result = str1 + " " + str2;
console.log(result); // "Hello world"
In this example, the +
operator is used to concatenate the strings str1
and str2
with a space in between, resulting in the string "Hello world"
.
2. Using the concat()
method
JavaScript also provides a built-in method called concat()
that can be used to concatenate strings. The concat()
method returns a new string that is the concatenation of the strings it is called on, as shown in the following example:
const str1 = "Hello";
const str2 = "world";
const result = str1.concat(" ", str2);
console.log(result); // "Hello world"
In this example, the concat()
method is called on the str1
string, and the str2
string is passed as an argument. The concat()
method joins the two strings with a space in between, resulting in the same output as the previous example.
3. Using template literals (Template strings)
Template literals, also known as template strings, are a feature introduced in ECMAScript 6 (ES6) that allows you to concatenate strings in a more readable and flexible way. Template literals use backticks (
) as delimiters and allow you to embed expressions inside the string using ${expression}
.
For example:
const str1 = "Hello";
const str2 = "world";
const result = `${str1} ${str2}`;
console.log(result); // "Hello world"
In this example, the expressions str1
and str2
are enclosed in ${}
inside the template literal, and they are evaluated and concatenated when the string is created. The resulting string is "Hello world"
.
4. Using the join() method
If you have an array of strings that you want to concatenate, you can use the join()
method, which is a built-in method of arrays in JavaScript. The join()
method concatenates the elements of an array into a string using a specified separator.
For example:
const arr = ["Hello", "world"];
const result = arr.join(" ");
console.log(result); // "Hello world"
In this example, the join()
method is called on the arr
array with a space as the separator, resulting in the same output as the previous examples.
5. Using assignment operators
JavaScript also provides assignment operators that can be used to concatenate strings and assign the result back to a variable in a concise way.
For example:
let str1 = "Hello";
const str2 = "world";
str1 += " " + str2;
console.log(str1); // "Hello world"
In this example, the +=
assignment operator is used to concatenate str2
to the end of str1
with a space in between, and the result is assigned back to str1
.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.