Is it best practice to use string concatenation or string interpolation? Why?

Question

Is it best practice to use string concatenation or string interpolation? Why?

Answer

String interpolation was a new feature of ES6 - string interpolation, using template literals, allows us to include variables and expressions in our strings, we can make multi-line strings without the need for an escape character, we can use apostrophes and quotes easily, they can make our strings and therefore our code easier to read as well. These are some of the reasons to use string interpolation over string concatenation. Also, if we choose to use ES6+ syntax, string interpolation would be better practice to use, in most cases, for the sake of consistency in our code.

4 Likes

A perfect answer! Thank you!

I was using string interpolation anyway bcs I am getting lazy to close the quote, add a + and add another open nd closed quotes with the sentence inside…not to mention that adding spaces can sometimes be a headache bcs you don’t always remember to add the spaces.

In string interpolation though, you don’t need to close and open for another sentence and just add the variables like this within the sentence like this ${variable} and keep on writing until the sentence is done! :slight_smile:

1 Like

why was string interpolation only available from ES6?

This is because of how ECMA script is implemented. The specification has to be accepted and then used on your platform. Notice when you browse the MDN there is a section that describes compatibility with your platform.

You can take a look at to much information on this here: ECMAScript 6 compatibility table

1 Like

I’m learning but I guess string interpolation is so much easier when concatenating strings and don’t have to worry much about escape characters. So add variables using ${ } and keep writing sentences without worrying about white spaces.

4 Likes