JS Template Literals
If you've worked with Javascript for a while you've probably hit the nightmare of string concatenation and how error prone the process is and how hard it is to troubleshoot if you're not careful. ```javascript var sentence = 'This is a very long sentence that we want to ' + 'concatenate together.' ``` In ES6/ES2015 there is a new way to create interpolated strings: [Template String Literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals). In this post we'll discuss three areas: - How to build template string literals and multi-line string literals - Variable interpolation - Possible ways to use template string literals for localization None of these things are new. You've always been able to do in Javascript with template literals it's now easier and more efficient to do so. ## Building Template Literals To build a Template Literal use the backtick (`` ` ``) character to open and close the string. In essence it's not different than concatenating strings but it allows you to create the string literals without worrying about whether you interpolated the correct type of quotation mark (`'` and `"`) or the `+` sign when creating multi-line strings. At it's simplest, an ES6 Template String Literal is written like this: ```javascript let demoString = `Hello World.` console.log(demoString); ``` We can also create Template Literal Strings using the same system. Also, note how we've been able to add angle brackets to open and close tags and single quotation marks to the longer example. ```javascript let longerString = `The second, greyed out example shown here shows the faux subscripts the browser creates by scaling whatever you put in the
tag; by using the sinf class from Utility OpenType instead, you’ll use the optically correct glyphs included in your web font. In browsers that don’t support the OpenType features, the browser default will still be used.`
console.log(longerString);
```
### Variable interpolation
Where the Template String Literals really show their strengths is when we interpolate variables in the longer string. Going back to our first example we'll add a variable to show the name of the person we're greeting:
```javascript
var userName2 = "carlos"
var greeting = `Hello, ${userName2}!`
console.log(greeting);
// Returns Hello, carlos!
```
In this example, the interpolation is the `${userName}` string that will take the value of the corresponding variable and put its name in placeholder.
We can also work with arrays as the source of interpolation data, something like the example below where we interpolate the values in the `userData` array:
```javascript
var userData = {
"name": "Carlos",
"home": "Chile",
};
var greeting2 = `Hello ${userData.name}!.
The weather in ${userData.home} is...`;
console.log(greeting2);
```
Using that last bit of code we can visit an interesting idea. Using String Template Literals when doing Translation.
```javascript
var userData = {
"en": {
"chapter": "Chapter",
"section": "Section",
},
"es": {
"chapter": "Capítulo",
"section": "Sección",
},
};
var chapterHeading = `${userData.en.chapter} 1, ${userData.en.section} 1.`;
console.log(chapterHeading);
// Produces: Chapter 1, Section 1.
var chapterHeadingEs = `${userData.es.chapter} 1, ${userData.es.section} 1.`;
console.log(chapterHeadingEs);
// Produces: Capítulo 1, Sección 1.
```
Using the code above we can also insert it in our HTML, looking something like this:
```