JavaScript Concatenate Strings

JavaScript Concatenate Strings | In this post, we will discuss how to concatenate strings in JavaScript. We will see how to concatenate two strings in JavaScript and how to concatenate string and variable in JavaScript.

There are various ways to concatenate strings in JavaScript:-

  1. concat() function
  2. + Operator
  3. Array#join()

In JavaScript, these are the main three ways to concatenate strings. We will discuss the various approaches and their relative merits in this tutorial.

JavaScript Concatenate Strings Using the concat() Function

There is a built-in concat() method for JavaScript strings. The concat() function returns a new string after concatenating the string parameters with the caller string. The returned string and the original string are unaffected by changes made to either.

The parameters are transformed to string values before concatenation if they are not of the string type. The concat() method is remarkably similar to the addition/string concatenation operators (+, +=), with the exception that concat() first converts its arguments to primitives before coercing them to strings.

Syntax-

  • concat(str1)
  • concat(str1, str2)
  • concat(str1, str2, /* …, */ strN)
let val = "Hello".concat(" ", "World", "!");
console.log(val);

Output:-

Hello World!

let val = "www".concat(".", "knowprogram", ".", "com");
console.log(val);

Output:-

www.knowprogram.com

The modified string is returned by the concat() function, which accepts one or more inputs. Since strings in JavaScript are immutable, concat() doesn’t actually change the string.

const str1 = 'Hello';
const str2 = str1.concat(' ', 'World');

console.log(str1);
console.log(str2);

Output:-

Hello
Hello World

Since strings cannot be changed, therefore “concat()” does not alter “str1”. We can get the original value of str1.

Concatenate Strings in JavaScript Using the + Operator

To combine two strings, use the same + operator that you would to add two numbers. 

const str = "Hello" + " " + "World" + "!";
console.log(str);

Output:-

Hello World!

Additionally, you can use +=, where a += b serves as a shortcut for a = a + b.

let str = 'Hello';
str += ' ';
str += 'World';
str += '!';
console.log(str);

Output:-

Hello World!

JavaScript Concatenate Strings Using Array#join()

The Array#join() function concatenates each element of an array to produce a new string. For instance:-

let str = ['Hello', 'World'].join(' ');
console.log(str);

Output:-

Hello World

How to Concatenate String and Variable in JavaScript

const str1 = 10;
const str2 = "Hello";

let val1 = str1 + "-Ten";
let val2 = str2 + " World";

console.log(val1);
console.log(val2);

Output:-

10-Ten
Hello World

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *