-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_strings.js
68 lines (52 loc) · 1.38 KB
/
template_strings.js
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
//////////////////////////
// ES6 - Template Strings
//////////////////////////
/*
// ES6
function getMessage() {
const year = new Date().getFullYear();
return 'The year is ' + year;
}
console.log(getMessage());
// ES6 with Template String
// Option 1
function getMessage() {
const year = new Date().getFullYear();
return `The year is ${year}`;
}
console.log(getMessage());
// Option 2
function getMessage() {
return `The year is ${new Date().getFullYear()}`;
}
console.log(getMessage());
*/
// =======================================================
const device_id = 4;
const guid = 20;
const username = 'Hello';
//ES5
const data = '{"device_id":"' + device_id + '","guid":"' + guid + '","username":"' + username + '","}';
console.log(data);
// ES6 with template String
const data2 = `{ "device_id": "${device_id}", "guid": "${guid}", "username": "${username}","}`;
console.log(data2);
// ==============================================
// Template Strings in Practice
// ES5
function doubleMessage(number) {
return "Your number doubled is " + (2 * number);
}
// ES6
function doubleMessage(number) {
return `Your number doubled is ${2 * number}`;
}
// =================================================
// ES5
function fullName(firstName, lastName) {
return firstName + lastName;
}
// ES6
function fullName(firstName, lastName) {
return `${firstName} ${lastName}`;
}