-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path051-template-literals-tagged.js
More file actions
184 lines (145 loc) · 5.14 KB
/
051-template-literals-tagged.js
File metadata and controls
184 lines (145 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// 1: THE OLD WAY - STRING CONCATENATION
// const name = "Sarah";
// const age = 28;
// const city = "NYC"
// Old Way
// const message = "Hello, my name is " + name + ". I am " + age + " years old and I live in " + city + "."
// console.log(message)
// const oldMultiLine = "This is line one.\n" +
// "This is line two.\n" +
// "This is line three.";
// console.log(oldMultiLine);
// 2: INTRODUCING TEMPLATE LITERALS
// const message = `Hello, my name is ${name}. I am ${age} years old and I live in ${city}.`;
// console.log(message)
// const multiLine = `This is line one.
// This is line two.
// This is line three.`;
// console.log(multiLine);
// 3: EXPRESSIONS IN TEMPLATE LITERALS
// const price = 99.99;
// const quantity = 3;
//Mathematical operations
// const total = `Total cost: $${price * quantity}`;
// console.log(total)
// Function calls
// const upperCase = `Your name in uppercase: ${name.toUpperCase()}`;
// console.log(upperCase); // Your name in uppercase: SARAH
// Conditional (ternary) operator
// const discount = 10;
// const finalMessage = `Your discount is ${discount > 0 ? 'active' : 'inactive'}`;
// console.log(finalMessage); // Your discount is active
// const user = {
// name: "John",
// premium: true
// };
// const greeting = `Welcome ${user.name}! ${user.premium ? `You have premium access` : `Upgrade to premium`}`;
// console.log(greeting);
// 4: PRACTICAL EXAMPLES
// HTML Generation:
// const product = {
// name: "Wireless Mouse",
// price: 29.99,
// rating: 4.5
// };
// const productCard = `
// <div class="product-card">
// <h3>${product.name}</h3>
// <p>Price: $${product.price}</p>
// <p>Rating: ${product.rating} stars</p>
// </div>
// `;
// console.log(productCard);
// Dynamic URLs:
// const userId = 12345;
// const apiEndpoint = "users";
// const apiUrl = `https://api.example.com/${apiEndpoint}/${userId}/profile`;
// console.log(apiUrl);
// https://api.example.com/users/12345/profile
// Formatting Data:
// const order = {
// id: "ORD-2025",
// items: 5,
// total: 149.99
// };
// const receipt = `
// Order ID: ${order.id}
// Items: ${order.items}
// Total: $${order.total.toFixed(2)}
// Thank you for your purchase!
// `;
// console.log(receipt);
// 5: TAGGED TEMPLATES - ADVANCED
function highlight(strings, ...values) {
console.log(strings); // Array of string parts
console.log(values); // Array of interpolated values
return strings.reduce((result, str, i) => {
return `${result}${str}${values[i] ? `<strong>${values[i]}</strong>` : ''}`;
}, '');
}
const name = "JavaScript";
const version = "ES6";
const result = highlight`Learning ${name} with ${version}`;
console.log(result);
// Learning <strong>JavaScript</strong> with <strong>ES6</strong>
function sanitize(strings, ...values) {
return strings.reduce((result, str, i) => {
const value = values[i] || '';
// Simple HTML escaping
const sanitized = String(value)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
return result + str + sanitized;
}, '');
}
const userInput = "<script>alert('XSS')</script>";
const safe = sanitize`User entered: ${userInput}`;
console.log(safe);
// User entered: <script>alert('XSS')</script>
function currency(strings, ...values) {
return strings.reduce((result, str, i) => {
const value = values[i];
const formatted = typeof value === 'number'
? `$${value.toFixed(2)}`
: value || '';
return result + str + formatted;
}, '');
}
const itemPrice = 19.5;
const taxAmount = 2.34;
const invoice = currency`Item: ${itemPrice}, Tax: ${taxAmount}, Total: ${itemPrice + taxAmount}`;
console.log(invoice);
// Item: $19.50, Tax: $2.34, Total: $21.84
// 6: ADVANCED TAGGED TEMPLATE EXAMPLE
function styled(strings, ...values) {
const styles = [];
let output = '';
strings.forEach((str, i) => {
output += str;
if (values[i] !== undefined) {
if (typeof values[i] === 'object' && values[i].style) {
output += `%c${values[i].text}%c`;
styles.push(values[i].style, ''); // Reset style after
} else {
output += values[i];
}
}
});
console.log(output, ...styles);
}
// Usage
styled`This is ${{ text: 'important', style: 'color: red; font-weight: bold' }} and this is ${{ text: 'highlighted', style: 'background: yellow' }}`;
// 7: COMMON GOTCHAS & BEST PRACTICES
// Gotcha #1: Backticks vs Quotes
const wrong = `Hello ${name}`
// Gotcha #2: Escaping Backticks
const message = `Use backticks \` for template literals`;
console.log(message) // Use backticks ` for template literals
// Good - simple expression
// const greeting = `Hello, ${userName}!`;
// Better - complex logic outside
// const discountPercent = calculateDiscount(user);
// const message = `Your discount: ${discountPercent}%`;
// Not recommended - too complex
// const bad = `Result: ${users.filter(u => u.active).map(u => u.name.toUpperCase()).join(', ')}`;