Joining Text

With commas, console.log() prints several values next to each other. Sometimes, though, you need to make one new text out of several pieces, for example to store it in a variable. For that, there is the + operator: it adds numbers and joins text. Programmers call joining text concatenation.

const day = "Saturday";
const message = "Today is " + day + ".";
console.log(message);

This prints:

Today is Saturday.

Unlike commas, + doesn't add any spaces: it joins the texts exactly as they are. You have to add the spaces yourself, inside the quotes. That's why there is a space after is in "Today is ". Without it, you'd get Today isSaturday.

A space on its own is text too: " ". It comes in handy when you join two variables:

const weekday = "Monday";
const partOfDay = "morning";
console.log(weekday + partOfDay);
console.log(weekday + " " + partOfDay);

This prints:

Mondaymorning
Monday morning

Instructions

Create a constant fullName: join firstName, a space and surname with +.

Print fullName. The output should be John Doe.

On the next line, join "User ", fullName and " has logged in." with +, and print the result. The program should print User John Doe has logged in.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

Back

9/15

Next