How to Concatenate Two Variables in JavaScript ?
Last Updated :
29 Jan, 2024
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);
|
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);
|
Share your thoughts in the comments
Please Login to comment...