-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstring-es6.js
66 lines (54 loc) · 1.49 KB
/
string-es6.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
/*
* Template literals
*
* Template literals provide a clean way to create strings and perform string interpolation.
* The syntax based on the dollar sign and curly braces ${..}.
* Template literals are enclosed by `backticks`.
*
*
* References
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
* https://www.smashingmagazine.com/2015/10/es6-whats-new-next-version-javascript/
*/
/*
* Syntax
*
* `string text`
*
* `string text line 1
* string text line 2`
*
*
* `string text ${expression} string text`
*/
// Multi-line strings
var welcomeMsg = `Hello dear,
You can see me at second line at your output`;
console.log(welcomeMsg)
// String interpolation
var name = "John",
age = 24,
hobby = "Programming";
var story = `
My name is ${name}.
I am ${age} years old and I love ${hobby}
Regards
THE LAST LINE
`;
console.log(story);
// ES5 equivalent
var es5_story = "My name is "+ name +"\nI am "+age+" years old and I love "+hobby+"\n\n"+"Regards\nTHE LAST LINE";
console.log("\n\nES5 equivalent:");
console.log(es5_story);
/*
* String Methods
*
* A couple of convenience methods have been added to the String prototype.
* Here is the simple demonstration:
*/
// Most of them basically eliminate some workarounds with the indexOf() method to achieve the same:
'my string'.startsWith('my'); //true
'my string'.endsWith('my'); // false
'my string'.includes('str'); // true
// create a repeating string
'my '.repeat(3); // 'my my my '