Horje
How to Concatenate Strings in JavaScript ?

String concatenation refers to combining multiple strings into a single string. This operation is commonly used in programming to create dynamic strings by joining text fragments or variables.

Approaches to Concate String in JavaScript:

Using String concat() Method

The concat() method is used to join two or more strings without changing the original strings and returning a new string.

Syntax:

str1.concat( str2, str3, . . . , strN )

Example: In this example, we will concat the given string using the strings concat() method.

JavaScript
// JavaScript concat() method to
// merge strings together

// Original string
let str = 'Geeks';

// Joining the strings together
let value = str.concat('for', 'Geeks');

console.log(value);

Output
GeeksforGeeks

Using JavaScript + Operator

The + operator adds strings and returns the concatenated string. It is the easiest method for the concatenation of two strings.

Syntax:

let str = str1 + str2

Example: In this example, we will concate two string using + operator.

JavaScript
// JavaScript + Operator to
// Merge Strings

// Original string
let str1 = 'Geeks';
let str2 = 'for';
let str3 = 'Geeks';

// Joining the strings together
let str = str1 + str2 + str3;

console.log(str);

Output
GeeksforGeeks

Using JavaScript Array join() Method

The JavaScript Array join() Method is used to join the elements of an array into a string. The elements of the string will be separated by a specified separator and its default value is a comma ( , ).

Syntax:

array.join( separator )

Example: In this article we will concate strings present in an array.

JavaScript
// JavaScript program to join Strings 
// using array join() method
let arr = ['Geeks', 'for', 'Geeks'];

// Joining the strings using
// join() method
let str = arr.join('');

console.log(str);

Output
GeeksforGeeks

Using Template Literals (Template Strings)

Template literals, denoted by backticks, allow string interpolation and multiline strings. Concatenation is achieved by embedding variables or expressions within `${}` placeholders. This approach enhances readability and simplifies string concatenation in JavaScript, offering a concise and flexible solution.

Example: In this example The variables firstName and lastName hold the strings “Suresh” and “Raina” respectively. fullName combines these variables using template literals to create the string “Suresh Raina”,

JavaScript
const firstName = "Suresh";
const lastName = "Raina";

const fullName = `${firstName} ${lastName}`;

console.log(fullName);

Output
Suresh Raina

Using String fromCharCode and reduce

This method involves using Array.from to convert strings into an array of character codes, then using reduce to concatenate them. Although less common, it’s a creative way to concatenate strings.

Example: In this example, we will concatenate two strings using the String.fromCharCode method and reduce.

JavaScript
function concatenate(...strings) {
  return String.fromCharCode(
    ...strings.reduce((acc, str) => acc.concat(...[...str].map(char => char.charCodeAt(0))), [])
  );
}

let str1 = 'Geeks';
let str2 = 'for';
let str3 = 'Geeks';
let str = concatenate(str1, str2, str3);
console.log(str);

Output
GeeksforGeeks



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Compute Iterative Power of a Number JavaScript Program to Compute Iterative Power of a Number
Replace Specific Words in a String Using Regular Expressions in JavaScript Replace Specific Words in a String Using Regular Expressions in JavaScript
JavaScript Program to Add n Binary Strings JavaScript Program to Add n Binary Strings
JavaScript Program to Print N to 1 using Recursion JavaScript Program to Print N to 1 using Recursion
JavaScript Program to Check for Palindrome Number JavaScript Program to Check for Palindrome Number

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
13