-
Notifications
You must be signed in to change notification settings - Fork 0
/
String_Interpolation.js
82 lines (72 loc) · 1.58 KB
/
String_Interpolation.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// We can write a String using "" OR ''
/*
ES6 onwards we have an advantage of the following...
1. If we want multiline String then we can use BACKTICK ``
2. String Interpolation `X is ${x} and Y is ${y}`; easy as compared with "X is "+x+" and Y is "+y;
*/
var a = "Amit"; // String in ""
undefined
typeof a;
'string'
a = 'Amit'; // String in ''
'Amit'
typeof a;
'string'
a = 'A'; // Nothing like character in JS
'A'
typeof a;
'string'
a = "Let's Do It "; // Advantage as we can apply ' directly in ""
"Let's Do It "
typeof a;
'string'
a = `gjdfkghkdjfgjkdf
fjhdsjkgdf
gjhdfkj
ghsdkj`;
'gjdfkghkdjfgjkdf\nfjhdsjkgdf\ngjhdfkj\nghsdkj'
console.log(a); // Multiline
gjdfkghkdjfgjkdf
fjhdsjkgdf
gjhdfkj
ghsdkj
undefined
var x = 10;
undefined
var y = 20;
undefined
"X is "+x+" and Y is "+y;
'X is 10 and Y is 20'
`X is ${x} and Y is ${y}`;
'X is 10 and Y is 20'
var t = true;
undefined
true + true;
2
true + false;
1
100 + true;
101
// More on String_Interpolation
// Real life example and advantage...
// when we want to give multiline output with changing values.... as we can't do this thing easily with ""
var candidateName = 'Ram';
var profile = 'SE';
var offerLetter = `Dear ${candidateName},
Your Job is Confirmed for ${profile}`;
undefined
console.log(offerLetter);
Dear Ram,
Your Job is Confirmed for SE
undefined
candidateName = 'Shyam';
'Shyam'
profile = 'DevOps';
'DevOps'
var offerLetter = `Dear ${candidateName},
Your Job is Confirmed for ${profile}`;
undefined
console.log(offerLetter);
VM421:1 Dear Shyam,
Your Job is Confirmed for DevOps
undefined