Beginner freeCodeCamp • introduction-to-strings

What Is String Concatenation, and How Can You Concatenate Strings with Variables?

Lesson Overview

**String Concatenation** is the process of joining two or more strings together to create a single, longer string.

String Concatenation is the process of joining two or more strings together to create a single, longer string.

Using the Plus Operator (+)

The simplest way to join strings is by using the + operator.

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

Concatenating with Variables

You can easily combine fixed strings with variables:

let name = "Antigravity";
let message = "Hello, " + name + "!";
console.log(message); // Output: "Hello, Antigravity!"

The Compound Addition Operator (+=)

You can also build a string over time using +=:

let intro = "I am a ";
intro += "developer.";
console.log(intro); // Output: "I am a developer."

Important Note on Spaces

JavaScript does not automatically add spaces between strings when you concatenate them. You must include the space yourself inside the quotes.

  • Incorrect: "Hello"+"World" -> "HelloWorld"
  • Correct: "Hello" + " " + "World" -> "Hello World"