Skip to content

Commit 531ed8c

Browse files
committed
Updated README: Adding examples of new string methods and destructuring
1 parent bad02b9 commit 531ed8c

File tree

1 file changed

+140
-1
lines changed

1 file changed

+140
-1
lines changed

README.md

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,143 @@ const arr = [1, 2, 3, 4, 5];
119119
const squares = arr.map(x => x * x); // Arrow Function for terser implementation
120120
```
121121

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+
const string = 'food';
140+
const substring = 'foo';
141+
console.log(string.includes(substring)); // true
142+
```
143+
144+
### .repeat()
145+
146+
```javascript
147+
function repeat(string, count) {
148+
var strings = [];
149+
while(strings.length < count) {
150+
strings.push(string);
151+
}
152+
return strings.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+
const name = 'Tiger';
187+
const age = 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 = new Date()
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.
231+
232+
### Destructuring Arrays
233+
234+
```javascript
235+
var arr = [1, 2, 3, 4];
236+
var a = arr[0];
237+
var b = arr[1];
238+
var c = arr[2];
239+
var d = arr[3];
240+
```
241+
242+
```javascript
243+
var [a, b, c, d] = [1, 2, 3, 4];
244+
console.log(a); // 1
245+
console.log(b); // 2
246+
```
247+
248+
### Destructuring Objects
249+
250+
```javascript
251+
var luke = { occupation: 'jedi', father: 'anakin' }
252+
var occupation = luke.occupation; // 'jedi'
253+
var father = luke.father; // 'anakin'
254+
```
255+
256+
```javascript
257+
var luke = { occupation: 'jedi', father: 'anakin' }
258+
var {occupation, father} = luke;
259+
console.log(occupation); // 'jedi'
260+
console.log(father); // 'anakin'
261+
```

0 commit comments

Comments
 (0)