How can I build/concatenate strings in JavaScript?

22    Asked by branwe_1737 in Java , Asked on Mar 18, 2025

You can concatenate strings in JavaScript using:

  • + operator: "Hello" + " World"

  • Template literals: `Hello ${name}` (recommended for readability)

  • concat() method: "Hello".concat(" World")

Template literals are the best choice for modern JavaScript.

Answered by Megan Hudson

String concatenation in JavaScript can be done in multiple ways, depending on readability and performance needs.

1. Using the + Operator (Simple & Common)

let greeting = "Hello" + " " + "World!";
console.log(greeting); // "Hello World!"

  • Easy and straightforward.
  • Works well for small and simple concatenations.

2. Using Template Literals (Best for Readability)

let name = "Alice";
let message = `Hello, ${name}! Welcome.`;
console.log(message); // "Hello, Alice! Welcome."

  • Uses backticks ` ` and ${} for variables.
  • Best for multi-line strings and dynamic values.

3. Using concat() Method (Less Common)

let str1 = "Hello";
let str2 = "World";
let result = str1.concat(" ", str2, "!");
console.log(result); // "Hello World!"

  • Useful when chaining multiple strings.
  • Not as commonly used as + or template literals.

4. Using join() with Arrays (For Multiple Strings)

let words = ["Hello", "World", "!"];
let sentence = words.join(" ");
console.log(sentence); // "Hello World !"

Best for joining multiple strings dynamically.

Which One to Use?

  • Use template literals for readability and dynamic content.
  • Use + for simple concatenations.
  • Use join() when dealing with arrays of strings.



Your Answer

Interviews

Parent Categories