Open In App

How to Concatenate Two Variables in JavaScript ?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, you can concatenate two variables (combine their values into a single string) using the + operator.

Example 1: Here, the + operator is used to concatenate the values of firstName, a space character, and lastName into the variable fullName.

Javascript




let firstName = "John";
let lastName = "Doe";
 
let fullName = firstName + " " + lastName;
console.log(fullName); // Outputs "John Doe"


Output

John Doe

Example 2: You can also use the += operator to concatenate and assign the result back to the variable.

Javascript




let firstName = "John";
let lastName = "Doe";
 
let fullName = firstName;
fullName += " " + lastName;
console.log(fullName); // Outputs "John Doe"


Output

John Doe


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads