ES6 template literals are a new way to create strings in JavaScript, which were introduced in ECMAScript 6 (ES6).
They provide a way to embed expressions into strings with a more readable syntax. Unlike traditional string concatenation, template literals preserve line breaks and white space, making the code more readable and easier to maintain. In this article, we'll discuss how to generate a string with ES6 template literals.
Template literals
Template literals are enclosed in backticks () instead of quotes ('' or ""). This allows you to embed expressions into the string by wrapping them in curly braces (${expression}`). The expression inside the curly braces can be any valid JavaScript expression, including variables, functions, and even other template literals.
One of the main benefits of using template literals is that they provide a way to create multi-line strings without having to use escape characters. This makes it easier to write and read code that spans multiple lines. For example, consider the following code that uses traditional string concatenation:
var name = "John"; var message = "Hello, " + name + "!\n" + "How are you today?"; console.log(message);
This code outputs the following string:
Hello, John! How are you today?
Now, consider the same code written using a template literal:
var name = "John"; var message = `Hello, ${name}! How are you today?`; console.log(message);
This code outputs the exact same string, but it's much easier to read and maintain, since it preserves the line breaks and white space.
Embedded expressions
Another benefit of template literals is that they provide a way to embed expressions into strings. For example, consider the following code that uses traditional string concatenation:
var name = "John"; var age = 32; var message = "My name is " + name + " and I am " + age + " years old."; console.log(message);
This code outputs the following string:
My name is John and I am 32 years old.
Now, consider the same code written using a template literal:
var name = "John"; var age = 32; var message = `My name is ${name} and I am ${age} years old.`; console.log(message);
This code outputs the exact same string, but it's much more readable and easier to maintain, since it embeds the expressions directly into the string.
Template literals also provide a way to perform string manipulation. For example, you can use the toUpperCase() method to convert a string to uppercase:
var name = "John"; var message = `Hello, ${name.toUpperCase()}!`; console.log(message);
This code outputs the following string:
Hello, JOHN!
Custom functions
Another useful feature of template literals is the ability to define custom functions that can be used within template literals. These functions are called "tagged templates." A tagged template is a function that is invoked before a template literal is evaluated. The function receives the following arguments:
- An array of strings, where each string represents a piece of the template literal.
- An array of expressions, where each expression represents a value that was embedded into the template literal.
Here's an example of a tagged template that adds a prefix and suffix to a string:
function prefixSuffix(strings, ...values) { let result = ""; for (let i = 0; i < values.length; i++) { result += strings[i] + values[i]; } result += strings[values.length]; return "Prefix: " + result + " Suffix"; } var name = "John"; var message = prefixSuffix`Hello, ${name}!`; console.log(message);
This code outputs the following string:
Prefix: Hello, John! Suffix
In this example, the prefixSuffix function takes the template literal and adds a prefix and a suffix to it. The function uses the strings and values arrays to build the final string. The strings array contains the pieces of the template literal that don't have expressions, and the values array contains the values of the expressions that were embedded into the template literal.
Conclusion
In conclusion, ES6 template literals provide a powerful and flexible way to create strings in JavaScript. They make it easier to write and read code that spans multiple lines, embed expressions into strings, and perform string manipulation. Additionally, they provide a way to define custom functions that can be used within template literals, making it possible to perform complex string manipulations. Whether you're a seasoned JavaScript developer or just starting out, template literals are a valuable tool to have in your toolkit.