-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplateString.js
More file actions
57 lines (45 loc) · 1.07 KB
/
templateString.js
File metadata and controls
57 lines (45 loc) · 1.07 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
// Multiple line strings
// String interpolation
"use strict"
var place = 'India';
var name = 'Lee Chong Wei';
var greet = "Hello Bengaluru my name is Tai Tzu ying!"
const bestPlayer = () => {
return 'Lin Dan!';
}
var greet1 = `Hello
Bengaluru
my
name
is
Tai Tzu ying`;
var greet3 = `Hello ${place} my name is ${name}!`;
var greet4 = `Hello ${place} my name is ${bestPlayer()}!`;
console.log('greet1 ', greet);
console.log('greet2 ', greet1);
console.log('greet3 ', greet3);
console.log('greet4 ', greet4);
// Template tag
// Example 1
function h1(strings) {
console.log(`<h1>${strings[0]}</h1>`);
}
h1`naveen`;
// Example 2
function complex(strings, ...values) {
console.log(strings);
console.log(values);
var body = '';
for(var i = 0; i < strings.length; i++ ) {
body += strings[i] + (values[i] || "");
}
console.log('In complex: ', body);
}
complex`Hello ${place} my name is ${name}!`;
// Example 3
function foo(strings, ...values) {
console.log(strings);
console.log(values);
console.log(strings[1])
};
foo`moo${foo}foo`