|
6 | 6 | | [Floating Point (Im)Precision](#floating-point-imprecision) | |
7 | 7 | | [The BigInt Type](#the-bigint-type) | |
8 | 8 | | [The Global "Number" & "Math" Objects](#the-global-number--math-objects) | |
| 9 | +| [Tagged Templates](#tagged-templates) | |
9 | 10 |
|
10 | 11 | ## How [`Numbers`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) Work and Behave in JS |
11 | 12 |
|
@@ -308,3 +309,62 @@ Some commonly used `Math's` methods: |
308 | 309 | console.log(Math.PI); // Output: 3.141592653589793 |
309 | 310 | console.log(Math.E); // Output: 2.718281828459045 |
310 | 311 | ``` |
| 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) |
0 commit comments