The course is part of this learning path
- Learn how to write a template literal string
- Learn how to use string expressions
- Learn how to use and understand the inner workings of tagged template literal functions
Note because I'm using single quotes to denote that this is a string, I need to escape the apostrophe where the letter S is located at. I will execute this and on the right, we can see the instructor's name is Farish, which matches my expectation.
The use of Template Literals allows for a more natural writing style. I'm gonna add to the console log by typing, and, and I'm gonna hit enter in order to fit this on the screen, but also to demonstrate something else about Template Literal. His email is, and now a new string expression, dollar sign, curly brackets, instructor.email. And I'm gonna go ahead and execute this, and if you look at the output, you'll notice that his email is, is on the following line. Template Literals will match how you write the string, what you see, how you wrote it is what you will get.
For further emphasis, I'm gonna add another line break before the email string expression. And the result on the right matches exactly how the string was written within the Literal.
Are there other use cases or options with Template Literals? Well, there is a advanced form of the Template Literal, referred to as either a tag Template Literal or a tag function. And this works by creating a function and then executing it with a template string. I will begin by creating an arrow function, const instructorInfo =, and I'm gonna give it three parameters. Sdr, which is short for string array, name and email. Inside of this function, I will constant log a template string with the string expressions of name and email.
For now, I'm gonna ignore the first parameter. Tag functions are invoked differently than traditional functions. I will write the function name and instead of passing through arguments, through parentheses, I need to use a template string. So beginning with back ticks and inside of the back ticks, I will pass through the string expressions of instructor.name and instructor.email. And when I execute this, the console log is displaying the name and email. So this leads to an interesting question.
How was the string array argument avoided? The answer is, it isn't avoided. I'm gonna go back into the function and console log str, and I'll execute this function again. And in the console, there is an array of empty string values. So what exactly is happening here? Well, let's take a deeper look. A tag function allows you to parse Template Literals. The purpose of the string array is to store text segments to be parsed by the Template Literal. What is a text segment? It is any block of text that comes before or after a string expression. The tag function has no text segments. Looking at the string array, any missing text segment is represented by an empty string as an element in the array. This allows for parsing of the Template Literal when a text segment doesn't exist allowing the function to work as normal.
Diving into the string array a little further, the first element of the string array always represents the texts that comes before the first string expression as shown here. Then the last element of the string array always represents the text that comes after the last string expression. The element in the middle of the string array is the text segment that comes between the first and second expression. Now this array skills based on the number of string expressions in the tag function.
The first and last elements will still represent the same text segments, but in between those two elements, there will be additional elements that will represent the text segment between each additional expression as shown on the screen. And you can access each text segment from the string array using that text segments index value. I will demonstrate this. Before I can demonstrate this, I need to add a couple text segments to my tag function. Before the instructor's name, I'm gonna add to the string, the instructor's name is. And inside of the string array is the segment of text that occurred before the first string expression. After the instructor's name string expression, I'm gonna add another text segment. And his email is, and I will execute this again, and we can see this as the middle value of the string array.
Another feature of Template Literals is the ability to execute operations within a string expression. On the screen I have a variable named value. That equals 10,000, and there's a function name double that will return the argument pass through times two. First using the string expression, I'm gonna console log the value variable, which equals 10,000.
Now using this as a basis, any operation can be performed inside of a string expression. I'm gonna multiply value by two. And when I execute this, the value is 20,000 and string expressions can work with direct mathematical operation. I'm gonna replace value with four, and on the right, the result is eight, meeting expectations. Also within string expressions, I can invoke functions. I will replace what's currently in the string expression with the function called double, passing in an argument of value. I will execute this again and to the right, there's a value of 20,000. And that's it. Thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.