How do you do string concatenation that also has a variable within the concatenation?

Though peculiar to ES6, I think, the string interpolation appears to be easier to implement than the regular concatenation especially when you have to include a variable or a number of variables within it (template expression).

How do you do concatenation using regular quotations that will include variables within it?

You would do it the same way as regular concatenation, but it is slightly more annoying than using string interpolation. Using this example, we could have the below:

const reportingForDuty = (rank, lastName) => `${rank} ${lastName} reporting for duty!`

const reportingForDuty2 = (rank, lastName) => rank + ' ' + lastName + ' reporting for duty!'

console.log(reportingForDuty('Private', 'Fido')) 
// Prints 'Private Fido reporting for duty!' to the console
console.log(reportingForDuty2('Private', 'Fido'))
// Prints 'Private Fido reporting for duty!' to the console

both of these return exactly the same thing, however you can see that string interpolation is more readable, and easier to work with as you don’t need to regularly add in the extra spaces and punctuation needed, you simply type like a normal sentence.


I think this is clear. Thanks for the clarification.

1 Like