Skip to content

Commit fbd9b9d

Browse files
committed
Replace string addition with Template Literals
Yes reviewers, ` \`\` \` \`\` ` is the only way to put backticks in backtics in Jekyll. I tried \
1 parent f6ea6dc commit fbd9b9d

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

js/lesson1/tutorial.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,15 @@ Change your `sayHello` function definition to look like this:
308308

309309
```js
310310
function sayHello(person) {
311-
console.log('Hello ' + person + '!');
311+
console.log(`Hello ${person}!`);
312312
}
313313
```
314314

315+
This is a **template literal** string and is denoted with the backtick
316+
`` ` `` symbol instead of `'`. Inside of a template literal, you can
317+
include **variables** with the `${variable}` syntax. This is called a
318+
**string interpolation**.
319+
315320
Now try calling it using `sayHello('Archibald')`.
316321

317322
> Try calling the function again using your own name?
@@ -337,7 +342,7 @@ like the others.
337342
In the body of this new function, write this line:
338343

339344
```js
340-
return 'Hello ' + person + '!';
345+
return `Hello ${person}!`;
341346
```
342347

343348
Try calling this function from the console. Look carefully at the
@@ -363,8 +368,8 @@ with commas. Change the first line of your `conversation` function to be:
363368
function conversation(person, topic) {
364369
```
365370
366-
Now add another line to the function that prints `"Do you like " +
367-
topic + "?"` on the console.
371+
Now add another line to the function that prints
372+
`` `Do you like ${topic}?` `` on the console.
368373
369374
Similarly, you call the function like this:
370375
@@ -536,7 +541,8 @@ You also know how to do all these things:
536541
537542
* Use the javascript console
538543
* Store values in variables
539-
* Add numbers and combine strings with `+`
544+
* Add numbers with `+`
545+
* Combine strings with template literals (`` ` ``) and `${}`
540546
* Define and call functions
541547
* Access object properties
542548
* Call object methods

js/lesson2/tutorial.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ In the first tutorial, we learnt all about **values**.
2121
```js
2222
const name = 'codebar';
2323

24-
console.log(name + ' is amazing!'); // this is an expression
24+
console.log(`${name} is amazing!`); // this is an expression
2525
```
2626

2727
**Numbers**:
2828

2929
```js
3030
const pi = 3.14;
3131

32-
console.log('The value of pi: ' + pi);
32+
console.log(`The value of pi: ${pi}`);
3333
```
3434

3535
and **Objects**:
@@ -39,7 +39,7 @@ const person = {
3939
first_name: 'Archibald'
4040
};
4141

42-
console.log('Hello ' + person.first_name + '!');
42+
console.log(`Hello ${person.first_name}!`);
4343
```
4444

4545
We are now going to introduce one more important type: **booleans**. A
@@ -49,8 +49,8 @@ boolean can only be `true` or `false`, for example:
4949
const codebarIsAwesome = true;
5050
const weatherIsAmazing = false;
5151

52-
console.log('Is codebar AWESOME? ' + codebarIsAwesome);
53-
console.log('Is the weather in London amazing? ' + weatherIsAmazing);
52+
console.log(`Is codebar AWESOME? ${codebarIsAwesome}`);
53+
console.log(`Is the weather in London amazing? ${weatherIsAmazing}`);
5454
```
5555

5656
## Expressions
@@ -65,19 +65,19 @@ const x = 6;
6565
const y = 3;
6666
const addition = x + y;
6767

68-
console.log('Addition: x + y = ' + addition); // Addition: x + y = 9
68+
console.log(`Addition: x + y = ${addition}`); // Addition: x + y = 9
6969

7070
const subtraction = x - y;
7171

72-
console.log('Subtraction: x - y = ' + subtraction); // Subtraction: x - y = 3
72+
console.log(`Subtraction: x - y = ${subtraction}`); // Subtraction: x - y = 3
7373

7474
const multiplication = x * y;
7575

76-
console.log('Multiplication: x * y = ' + multiplication); // Multiplication: x * y = 18
76+
console.log(`Multiplication: x * y = ${multiplication}`); // Multiplication: x * y = 18
7777

7878
const division = x / y;
7979

80-
console.log('Division: x / y = ' + division); // Division: x / y = 2
80+
console.log(`Division: x / y = ${division}`); // Division: x / y = 2
8181
```
8282

8383
> Why not try some other maths problem using the `x` and `y` variables?
@@ -97,19 +97,19 @@ let x = 5;
9797
let y = 3;
9898
const modulus = x % y;
9999

100-
console.log('Remainder: x % y = ' + modulus);
100+
console.log(`Remainder: x % y = ${modulus}`);
101101

102102
const exponentiation = x ** y;
103103

104-
console.log('Exponentiation: x ** y = ' + exponentiation);
104+
console.log(`Exponentiation: x ** y = ${exponentiation}`);
105105

106106
const increment = x++;
107107

108-
console.log('Increment: x++ = ' + increment);
108+
console.log(`Increment: x++ = ${increment}`);
109109

110110
const decrement = y--;
111111

112-
console.log('Decrement: y-- = ' + decrement);
112+
console.log(`Decrement: y-- = ${decrement}`);
113113
```
114114

115115
#### Comparisons
@@ -122,7 +122,7 @@ const oranges = 'oranges';
122122

123123
const equal = apples === oranges;
124124

125-
console.log('Apples and oranges are the exactly same: ' + equal);
125+
console.log(`Apples and oranges are the exactly same: ${equal}`);
126126
```
127127

128128
The opposite of `===` is `!==`. It returns `true` if they are not equal, and `false` if they are.
@@ -133,7 +133,7 @@ const oranges = 'oranges';
133133

134134
const notEqual = apples !== oranges;
135135

136-
console.log('Apples and oranges are different: ' + notEqual);
136+
console.log(`Apples and oranges are different: ${notEqual}`);
137137
```
138138

139139
> You may also see `==` and `!=`, these are similar but have some quirks so it's generally recommended to avoid them.
@@ -148,10 +148,10 @@ const students = 24;
148148
const pizzas = 25;
149149

150150
const moreStudents = students > coaches;
151-
console.log('Are there more students than coaches?' + moreStudents);
151+
console.log(`Are there more students than coaches? ${moreStudents}`);
152152

153153
const lessStudents = students < pizzas;
154-
console.log('Are there fewer students than pizzas?' + lessStudents);
154+
console.log(`Are there fewer students than pizzas? ${lessStudents}`);
155155

156156
```
157157
> Play around with changing the `coaches`, `students` and `pizzas` variable numbers to familiarise yourself with operators.
@@ -160,7 +160,7 @@ You can also combine operators.
160160

161161
```js
162162
const enoughPizzas = (coaches + students) < pizzas;
163-
console.log('Do we have enough pizzas for everybody? ' + enoughPizzas);
163+
console.log(`Do we have enough pizzas for everybody? ${enoughPizzas}`);
164164
```
165165
> Now sit with your coach and create 2 variables, one is your age and one is the
166166
> minimum driving age. Then console log whether you are old enough to drive.
@@ -256,7 +256,7 @@ while (i <= 10) {
256256
i = i + 1;
257257
}
258258

259-
console.log('Total: ' + total);
259+
console.log(`Total: ${total}`);
260260
```
261261

262262
> We can also express `<= 10` using `< 11`
@@ -299,7 +299,7 @@ for (i = 1; i <= 10; i = i + 1) {
299299
total = total + i;
300300
}
301301

302-
console.log('Total: ' + total);
302+
console.log(`Total: ${total}`);
303303
```
304304
> Another way to write the for loop is `for (let i = 1; i <= 10; i++)`. The `i++` is a short way of writing "increase i by one".
305305
@@ -577,7 +577,7 @@ function displayPopulation() {
577577
const paragraph = document.createElement('p');
578578

579579
// Make some text content to put into your <p></p>
580-
const content = document.createTextNode('Population: ' + london.population);
580+
const content = document.createTextNode(`Population: ${london.population}`);
581581

582582
// Put the text content into the <p></p>.
583583
paragraph.appendChild(content);

js/lesson3/tutorial.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ wrote on the console earlier, but instead of adding 'My first item',
9898
we want to add the parameter to this function:
9999

100100
```js
101-
'<li>' + item + '</li>'
101+
`<li>${item}</li>`
102102
```
103103

104104
Once you've done that, reload `index.html` in your browser, and try
@@ -257,8 +257,7 @@ Store those lengths in two variables called `pending` and `completed`.
257257

258258
Now let's display them on the page. Look at `index.html` and find the
259259
span that comes just below the `<ol>` element. Write a jQuery selector
260-
for that element, and then call `.text('Pending: ' + pending + '
261-
Completed: ' + completed)` on it.
260+
for that element, and then call `` `.text(`Pending: ${pending} Completed: ${completed}`)` `` on it.
262261

263262
### Write some code
264263

@@ -425,7 +424,7 @@ method to add something to the start of an element, and something like
425424
this to make the new box:
426425

427426
```js
428-
'<div class="item" style="background-color: ' + color + ';"></div>'
427+
`<div class="item" style="background-color: ${color};"></div>`
429428
```
430429

431430
> Run `addBox('FF0033')` from the console to make sure your code works.

js/lesson4/tutorial.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ $(document).ready(function() {
109109
const input = $(this);
110110
const username = input.val();
111111

112-
console.log('username was: ' + username);
112+
console.log(`username was: ${username}`);
113113
}
114114
});
115115
});
@@ -119,7 +119,7 @@ Now we're ready to pass this through to GitHub. Let's make another function, som
119119

120120
```js
121121
function getGithubInfo(username) {
122-
const url = 'https://api.github.com/users/' + username;
122+
const url = `https://api.github.com/users/${username}`;
123123

124124
const xmlhttp = new XMLHttpRequest();
125125
xmlhttp.open('GET', url, false);
@@ -152,7 +152,7 @@ Our `getGithubInfo` method will return the response from the server, including t
152152

153153
```js
154154
function getGithubInfo(username) {
155-
const url = 'https://api.github.com/users/' + username;
155+
const url = `https://api.github.com/users/${username}`;
156156

157157
const xmlhttp = new XMLHttpRequest();
158158
xmlhttp.open('GET', url, false);

0 commit comments

Comments
 (0)