JS Coding Questions Logo
JS Coding Questions
#313💼 Interview💻 Code

What are tagged templates

Advertisement

728x90

Tagged templates are the advanced form of templates in which tags allow you to parse template literals with a function. The tag function accepts the first parameter as an array of strings and remaining parameters as expressions. This function can also return manipulated strings based on parameters. Let's see the usage of this tagged template behavior of an IT professional skill set in an organization,

javascript
1var user1 = "John";
2
3var skill1 = "JavaScript";
4
5var experience1 = 15;
6
7var user2 = "Kane";
8
9var skill2 = "JavaScript";
10
11var experience2 = 5;
12
13function myInfoTag(strings, userExp, experienceExp, skillExp) {
14  var str0 = strings[0]; // "Mr/Ms. "
15  var str1 = strings[1]; // " is a/an "
16  var str2 = strings[2]; // "in"
17
18  var expertiseStr;
19  if (experienceExp > 10) {
20  expertiseStr = "expert developer";
21  } else if (skillExp > 5 && skillExp <= 10) {
22  expertiseStr = "senior developer";
23  } else {
24  expertiseStr = "junior developer";
25  }
26
27  return `${str0}${userExp}${str1}${expertiseStr}${str2}${skillExp}`;
28}
29
30var output1 = myInfoTag`Mr/Ms. ${user1} is a/an ${experience1} in ${skill1}`;
31
32var output2 = myInfoTag`Mr/Ms. ${user2} is a/an ${experience2} in ${skill2}`;
33
34console.log(output1); // Mr/Ms. John is a/an expert developer in JavaScript
35
36console.log(output2); // Mr/Ms. Kane is a/an junior developer in JavaScript

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 56

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
313of476