Template literals, introduced in ECMAScript 6 (ES6), are a way to work with strings more efficiently and expressively in JavaScript.
Syntax: Te...
For further actions, you may consider blocking this person and/or reporting abuse
Hi Fatemeh, thanks for the article! I just had a couple of comments.
I was a little confused by your tagged function example - is it doing exactly the default behaviour for template literals is? I've not seen this functionality before, so breaking down the example further (or providing an example that isn't the default behaviour) would be hugely appreciated!
Secondly, my understanding of nested templates is a little different to the example you provide - your example seems to just be passing a template literal into another function as a string (or am I missing something here, is it not passed as a string and does this provide performance benefits I'm not seeing?)
The MDN article you link to lists something I'd more readily associate with "nesting":
Here the second template literal is actually inside the first.
Again, thanks for the article - template literals aren't something I've investigated beyond the basic usage so far, so thanks! Just wanted to clarify these two points.
Joe
You are right, In my example, the behavior is like default.
Tagged templates are like upgraded versions of template literals. They let you use a function to handle the template, rather than just seeing it as a simple string. This gives you more say over how the strings and expressions inside are put together. To use a tagged template, you write it a bit like calling a function with something in parentheses. But instead of using regular parentheses, you use a template literal. The function you use to handle the template is called a "tag function".
Understanding the difference between template literals and tagged templates is key:
Template literals work by turning into a string, with expressions put right into the string.
Tagged templates work differently. They send both the literal strings and the results of the expressions to a function. This function can then change and give them back in a unique way.
Tagged templates provide more freedom and control over how strings are made, making them strong tools for tricky string handling.