Code highlights logo



String Concatenation with Variables

In JavaScript, we often need to combine strings together to create meaningful outputs or dynamic messages. This process is called string concatenation. We can concatenate strings with variables or with other strings.

The most common way to concatenate strings with variables is by using the + operator. This operator can be used to join strings together.

1let firstName = "John";
2let lastName = "Doe";
4let fullName = firstName + " " + lastName;
5console.log(fullName); // Output: John Doe

In the above example, we have two variables firstName and lastName. By using the + operator, we can merge the values of these variables along with a space between them to create the fullName variable. Finally, we output the fullName variable to the console.

It's important to note that when concatenating with the + operator, we need to make sure to include any necessary spacing or punctuation marks within the string.



Declare a new variable age and assign it a value of 25.


Create a new variable message and concatenate the firstName variable with the string ", aged " and the age variable. Assign the result to the message variable.


Use the console.log() function to print out the message variable.

Sign up to start coding

Already have an account?

Sign In

Course content