You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constsquares=arr.map(x=> x * x); // Arrow Function for terser implementation
120
120
```
121
121
122
-
> **Best Practice**: Use **Arrow Functions** in place of function expressions when possible.
122
+
> **Best Practice**: Use **Arrow Functions** in place of function expressions when possible.
123
+
124
+
## Strings
125
+
126
+
With ES6, the standard library has grown immensely. Along with these changes are new methods which can be used on strings, such as **.includes()** and **.repeat()**.
127
+
128
+
### .includes()
129
+
130
+
```javascript
131
+
var string ='food';
132
+
var substring ='foo';
133
+
console.log(string.indexOf(substring) >-1);
134
+
```
135
+
136
+
Instead of checking for a return value `> -1` to denote string containment, we can simply use **.includes()** which will return a boolean:
137
+
138
+
```javascript
139
+
conststring='food';
140
+
constsubstring='foo';
141
+
console.log(string.includes(substring)); // true
142
+
```
143
+
144
+
### .repeat()
145
+
146
+
```javascript
147
+
functionrepeat(string, count) {
148
+
var strings = [];
149
+
while(strings.length< count) {
150
+
strings.push(string);
151
+
}
152
+
returnstrings.join('');
153
+
}
154
+
```
155
+
156
+
In ES6, we now have access to a terser implementation:
157
+
158
+
```javascript
159
+
// String.repeat(numberOfRepetitions)
160
+
'meow'.repeat(3); // 'meowmeowmeow'
161
+
```
162
+
163
+
### Template Literals
164
+
165
+
Using **Template Literals**, we can now construct strings that have special characters in them without needing to escape them explicitly.
166
+
167
+
```javascript
168
+
var text ="This string contains \"double quotes\" which are escaped."
169
+
```
170
+
171
+
```javascript
172
+
let text =`This string contains "double quotes" which are escaped.`
173
+
```
174
+
175
+
**Template Literals** also support interpolation, which makes the task of concatenating strings and values:
176
+
177
+
```javascript
178
+
var name ='Tiger';
179
+
var age =13;
180
+
console.log('My cat is named '+ name +' and is '+ age +' years old.');
181
+
```
182
+
183
+
Much simpler:
184
+
185
+
```javascript
186
+
constname='Tiger';
187
+
constage=13;
188
+
console.log(`My cat is named ${name} and is ${age} years old.`);
189
+
```
190
+
191
+
In ES5, we handled new lines as follows:
192
+
193
+
```javascript
194
+
var text = (
195
+
'cat\n'+
196
+
'dog\n'+
197
+
'nickelodeon'
198
+
)
199
+
```
200
+
201
+
Or:
202
+
203
+
```javascript
204
+
var text = [
205
+
'cat',
206
+
'dog',
207
+
'nickelodeon'
208
+
].join('\n')
209
+
```
210
+
211
+
**Template Literals** will preserve new lines for us without having to explicitly place them in:
212
+
213
+
```javascript
214
+
var text = (
215
+
`cat
216
+
dog
217
+
nickelodeon`
218
+
)
219
+
```
220
+
221
+
**Template Literals** can accept expressions, as well:
222
+
223
+
```javascript
224
+
let today =newDate()
225
+
let text =`The time and date is ${today.toLocaleString()}`
226
+
```
227
+
228
+
## Destructuring
229
+
230
+
Destructuring allows us to extract values from arrays and objects (even deeply nested) and store them in variables with a more convient syntax.
0 commit comments