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
Copy file name to clipboardExpand all lines: README.md
+42-7Lines changed: 42 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,27 +7,62 @@ Welcome to another JavaScript lab!
7
7
8
8
## Objectives
9
9
10
+
In this lab, we're going to work with strings. Remember, strings in JavaScript are wrapped in single or double quotes, or in back ticks.
11
+
12
+
By the end of this lab, you'll
13
+
10
14
1. Manipulate strings by hand in JavaScript
11
15
2. Practice interpolating with template literals
12
16
13
17
## Introduction
14
18
15
-
In this lab, we're going to work with strings. Remember, strings in JavaScript are wrapped in single or double quotes, or in back ticks.
19
+
Imagine we're planning a birthday party for Bill Nye. There are going to be a lot of people there, so we're going to use JavaScript to help us keep everything straight.
20
+
21
+
First, we need to practice greeting everyone. (I don't know about you, but I sometimes get nervous and say the dumbest things — but we don't want to embarrass ourselves in front of Bill Nye!)
22
+
23
+
One might think that we could just type
24
+
25
+
```javascript
26
+
Hello, everybody!
27
+
```
28
+
29
+
in our browser's console and be done with it. Give it a try.
30
+
31
+
You should see something like
32
+
33
+
```shell
34
+
Uncaught ReferenceError: Hello is not defined(…)
35
+
```
36
+
37
+
Well, that won't work. (This is why we practice!) In order to greet our guests, we need to tell JavaScript that we're using a **string**. A string is a collection of characters (letters, numbers, and symbols) wrapped in single or double quotes (or, as we'll see, in back ticks). So to greet everyone, we can write,
38
+
39
+
```javascript
40
+
'Hello, everybody!'
41
+
```
42
+
43
+
or
44
+
45
+
```javascript
46
+
"Hello, everybody!"
47
+
```
48
+
49
+
They're the same in this case.
16
50
17
-
When we wrap strings in single or double quotes, we can join them together using the `+` operator:
51
+
What if we want to say hi to a special guest, like Neil deGrasse Tyson? When we wrap strings in single or double quotes, we can join them together using the `+` operator:
18
52
19
53
```javascript
20
-
"Hello, "+"world!"// "Hello, world!"
54
+
var specialGuest ="Neil deGrasse Tyson"
55
+
"Hello, "+ specialGuest +"!"// "Hello, Neil deGrasse Tyson!"
21
56
```
22
57
23
-
This is called _concatenation_.
58
+
This is called _concatenation_. Notice that `specialGuest` is _also_ a string!
24
59
25
-
Similarly, when we wrap strings in back ticks, we can use placeholders (`${}`) and insert variables or evaluated JavaScript directly:
60
+
When we wrap strings in back ticks, we can use placeholders (`${}`) and insert variables or evaluated JavaScript directly:
26
61
27
62
```javascript
28
-
varthing='world'
63
+
varspecialGuest="Neal deGrasse Tyson"
29
64
30
-
`Hello, ${thing}! High ${3+2}!`// "Hello, world! High 5!"
65
+
`Hello, ${specialGuest}! High ${3+2}!`// "Hello, Neil deGrasse Tyson! High 5!"
0 commit comments