Skip to content

Commit 9d10e23

Browse files
committed
learn about Tagged Template
1 parent e35dfd8 commit 9d10e23

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

More-on-Numbers-and-Strings/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| [Floating Point (Im)Precision](#floating-point-imprecision) |
77
| [The BigInt Type](#the-bigint-type) |
88
| [The Global "Number" & "Math" Objects](#the-global-number--math-objects) |
9+
| [Tagged Templates](#tagged-templates) |
910

1011
## How [`Numbers`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) Work and Behave in JS
1112

@@ -308,3 +309,62 @@ Some commonly used `Math's` methods:
308309
console.log(Math.PI); // Output: 3.141592653589793
309310
console.log(Math.E); // Output: 2.718281828459045
310311
```
312+
313+
## [Tagged Templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)
314+
315+
A Tagged Template is a feature that allows you to customize how template literals (strings with embedded expressions) are processed. When you use a tagged template, you can apply a function (the tag) to the template literal, and this function has the ability to modify the resulting string based on the interpolated values and the template itself.
316+
317+
***To create a tagged template, you place the function name (the tag) immediately before the template literal using backticks (``).***
318+
319+
Here's the syntax:
320+
321+
```javascript
322+
function tagFunction(strings, ...values) {
323+
// Custom logic to process the strings and values
324+
// Return the modified string
325+
}
326+
327+
const result = tagFunction`template literal with embedded ${values}`;
328+
```
329+
330+
The `tagFunction` takes two arguments:
331+
332+
1. `strings`: An array containing the template literal's static parts (text without interpolated values).
333+
334+
2. `...values`: The values of the interpolated expressions.
335+
336+
Let's see an example to illustrate how tagged templates work:
337+
338+
```javascript
339+
function upperCaseTag(strings, ...values) {
340+
let result = "";
341+
for (let i = 0; i < strings.length; i++) {
342+
result += strings[i];
343+
if (i < values.length) {
344+
result += String(values[i]).toUpperCase();
345+
}
346+
}
347+
return result;
348+
}
349+
350+
const name = "Alice";
351+
const age = 30;
352+
353+
const output = upperCaseTag`Hello, my name is ${name} and I am ${age} years old.`;
354+
355+
console.log(output); // Hello, my name is ALICE and I am 30 years old.
356+
```
357+
358+
In the above example, we defined a `upperCaseTag` function as the tag for the template literal. This function iterates over the `strings` array and appends each static part to the `result` string. If there is a corresponding value at that index, it converts the value to uppercase and appends it to the `result` string.
359+
360+
When we use the `upperCaseTag` function with the template literal, it modifies the interpolated values (name and age) to be in uppercase.
361+
362+
Tagged templates are powerful because they allow you to customize how template literals are processed and can be used for various purposes like internationalization, escaping HTML, or other custom formatting requirements.
363+
364+
Readings:
365+
366+
- [Tagged templates - MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates)
367+
368+
- [Tagged Template Literals](https://wesbos.com/tagged-template-literals)
369+
370+
- [Magic of Tagged Templates Literals in JavaScript?](https://patelhemil.medium.com/magic-of-tagged-templates-literals-in-javascript-e0e2379b1ffc)

More-on-Numbers-and-Strings/summary-with-code/app.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,24 @@ function randomIntBetween(min, max) {
1717
return Math.floor(Math.random() * (max - min + 1) + min);
1818
}
1919

20-
console.log(randomIntBetween(30, 89));
20+
console.log(randomIntBetween(30, 89));
21+
22+
// Tagged Templates
23+
24+
function productDescription(strings, productName, productPrice) {
25+
console.log(strings);
26+
console.log(productName);
27+
console.log(productPrice);
28+
let priceCategory = 'pretty cheap regarding its price';
29+
if (productPrice > 20) {
30+
priceCategory = 'fairly priced';
31+
}
32+
// return `${strings[0]}${productName}${strings[1]}${priceCategory}${strings[2]}`; // The product (JavaScript Course) is fairly priced.
33+
return {name: productName, price: prodPrice}; // {name: 'JavaScript Course', price: 29.99}
34+
}
35+
36+
const prodName = 'JavaScript Course';
37+
const prodPrice = 29.99;
38+
39+
const productOutput = productDescription`The product (${prodName}) is ${prodPrice}.`;
40+
console.log(productOutput);

0 commit comments

Comments
 (0)