Open In App

How to Concatenate Two Variables in JavaScript ?

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.






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.






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

Output
John Doe

Article Tags :