-
Notifications
You must be signed in to change notification settings - Fork 4
/
1-slides.html
1453 lines (907 loc) · 35 KB
/
1-slides.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>JavaScript First Steps</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/4.3.1/reset.min.css" integrity="sha512-Mjxkx+r7O/OLQeKeIBCQ2yspG1P5muhAtv/J+p2/aPnSenciZWm5Wlnt+NOUNA4SHbnBIE/R2ic0ZBiCXdQNUg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/4.3.1/reveal.min.css" integrity="sha512-ITH3NSfntO7uI5n+BnxGNXpzDUoOsmAXuG37UDONLxNYIdc0EBBOOQ1xyc+t9ag9ETSuBXFApx+Rod0uURfDYw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/4.3.1/theme/night.min.css" integrity="sha512-bfCyfT+5Js2CKjpiF7yCtTcNlNTtcdAIHV8DfGLCQuTfTEHTU9uEvhQ/Ty4zv3IRtkFQu8DKeM2MHGka+K/cxA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<!-- Theme used for syntax highlighted code -->
<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/night-owl.min.css" integrity="sha512-i5X6Fdn/ZqvGSqPrdMa3FgcpXM/Nr6YccSKFYT93zljl/HZDEpvBbE5Pxp91eiWGccZLrL/LDQJd7fjTRYsVaA==" crossorigin="anonymous" referrerpolicy="no-referrer" /> -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/a11y-dark.min.css" integrity="sha512-Vj6gPCk8EZlqnoveEyuGyYaWZ1+jyjMPg8g4shwyyNlRQl6d3L9At02ZHQr5K6s5duZl/+YKMnM3/8pDhoUphg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
.reveal code {
color: var(--r-link-color-hover);
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-markdown>
<textarea data-template>
## JavaScript First Steps
### A [FrontendMasters](https://frontendmasters.com) course by [AnjanaVakil](https://twitter.com/AnjanaVakil)
[anjana.dev/javascript-first-steps](https://anjana.dev/javascript-first-steps)
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
### About this course
- Taking our *first steps* in an infinite journey
- Hands-on projects
- Just-in-time vs. just-in-case learning
--
### Grand plan
<span style="color:var(--r-background-color);">time</span> | Part 1 | Part 2 | Part 3
--- | --- | --- | ---
topics | DOM <br> datatypes <br> arrays & objects | functions <br> events <br> branches & loops | (a)sync <br> pro syntax <br> bugs & errors
project | tic tac toe | quiz game | doggos
--
### Ground rules
- Everyone belongs here!
- There are no "dumb" questions
- Growth > Goalposts
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n" >
<textarea data-template>
# Part 1
--
### Goals
- Get hands-on with JS
- Explore & modify a web page
- Meet common data types & operators
- Declare variables to remember values
- Collect values into objects & arrays
- Play a Tic-Tac-Toe game in the browser!
--
### Ready?
# Let's go!
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
## JavaScript!
### What? Why? How?
--
### What is JS?
--
```js
let js = {
name: "JavaScript",
abbreviation: "JS",
isAwesome: true,
officialSpec: "ECMAScript",
birthYear: 1995,
creator: "Brendan Eich"
};
if (js.isAwesome) {
shout("hell yes JS!");
}
```
--
### Why do we need JS?
--
## HTML & CSS & JS: BFFs!
--
<img src="https://media4.giphy.com/media/3o6gE8sp8lYiUBgx8Y/giphy.gif?cid=ecf05e47qgttnhw10sv7okbd6osgrjh1kb2pubtrdazuwtoi&rid=giphy.gif&ct=g" alt="Hamilton GIF by Tony Awards via Giphy" width=700 />
--
### How do we write JS?
--
<img src="https://media1.tenor.com/images/bbb7a06768e4f3c507632383b73eb9a1/tenor.gif?itemid=9213220" alt="Gif of girls performing 'light as a feather, stiff as a board' ritual in 'The Craft'" />
--
<img src="http://1.bp.blogspot.com/-mULfDILNkLQ/Uecl0_H_6hI/AAAAAAAAJiI/vR4dwnwrphs/s1600/the-craft-the-craft-20560404-1842-1219.jpg" alt="Still of girls performing summoning ritual in a circle of candles on the beach, from 'The Craft'" />
--
### ~~How~~ *Where* do we write JS?
--
- **The browser's JS `console`**
- Local text file in editor, e.g. TextEdit, [VS Code](https://code.visualstudio.com/)
- Online playground e.g. [CodePen](https://codepen.io), [CodeSandbox](https://codesandbox.io)
--
## Let's do it!
1. Open [1-tictactoe.html](./1-tictactoe.html) in your browser<br>(`File` > `Open file...`)
1. Inspect the page to open DevTools<br>(Right click > `Inspect`)
1. Click on `Console` tab
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n" data-auto-animate>
<textarea data-template>
## The DOM
### Document Object Model
--
### A simple `document`
```html [ 2-8 | 3 | 4-7 | 5-6]
<!DOCTYPE html>
<html lang="en-US">
<head>...</head>
<body>
<header>...</header>
<div id="board">...</div>
</body>
</html>
```
Let's draw the DOM tree!
--
### Reading the DOM with JS
Let's dig into this `document`!
--
```js
document
```
<p class="fragment">the entire HTML document</p>
--
```js
document.title
```
<p class="fragment">the page (document) title</p>
--
```js
document.body
```
<p class="fragment">the <code>body</code> element</p>
--
```js
document.body.children
```
<p class="fragment">all the elements within the <code>body</code></p>
--
```js
document.getElementById("board")
```
<div class="fragment" data-fragment-index="2"><pre class="js hljs"><code>document.querySelector("#board")</code></pre></div>
<p class="fragment" data-fragment-index="1">the (first) element with <code>id="board"</code></p>
--
```js
document.getElementsByTagName("h1")
```
<div class="fragment" data-fragment-index="2"><pre class="js hljs"> <code>document.querySelectorAll("h1")</code></pre></div>
<p class="fragment" data-fragment-index="1">all the <code>h1</code> elements</p>
--
```js
document.getElementsByClassName("player")
```
<div class="fragment" data-fragment-index="2"><pre class="js hljs"><code>document.querySelectorAll(".player")</code></pre></div>
<p class="fragment" data-fragment-index="1">all the elements with <code>class="player"</code> </p>
--
<pre class="r-fit-text js hljs"><code>document.getElementsByClassName("player").length</code></pre>
<pre class="r-fit-text js hljs"> <code>document.querySelectorAll(".player").length</code></pre>
<p class="fragment" >the number of elements with <code>class="player"</code> </p>
--
<pre class="r-fit-text js hljs"><code>document.getElementById("p1-name").textContent</code></pre>
<p class="fragment">the text inside the element with <code>id="p1-name"</code></p>
--
### *Do you expect me to remember all this!?!??!*
<img src="https://gifimage.net/wp-content/uploads/2017/08/overwhelmed-gif-6.gif" alt="Gif of Steve Urkel looking overwhelmed" />
--
The three most important letters in web development:
# ✨MDN✨
[developer.mozilla.org](https://developer.mozilla.org)
--
## Exercise
Type commands in the console to retrieve:
1. all the `p` elements
1. the text "X"
1. the number of squares in the board
1. the text "A game you know"
--
### Editing the DOM with JS
--
```js
document.title = "My Page"
```
<p class="fragment">change the page title</p>
<p class="fragment" style="font-style:italic;">note the double-quotes</p>
--
<pre class="r-fit-text js hljs"><code>document.getElementById("p1-name").textContent = "Sofia"</code></pre>
<p class="fragment">replace the text of the <code>#p1-name</code> element</p>
--
<pre class="r-fit-text js hljs"><code>document.getElementById("p1-name").append(" & friends")</code></pre>
<p class="fragment">add to the end of the element's current text</p>
--
## Exercise
1. Change the player names to you & neighbor
1. Swap the player symbols
1. Change subtitle to "A game you know and love"
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
## Values & Data Types
--
### What are these?
`"Tic Tac Toe"`
`9`
`"#board"`
--
# Values
<p class="fragment">chunks of information we want to work with</p>
<p class="fragment">that information (data) can be of different types</p>
--
## text data
e.g. `"Tic Tac Toe"` or `"#board"`
<p class="fragment">aka...</p>
--
## strings
<img src="https://i0.wp.com/www.muffinchanel.com/wp-content/uploads/2020/09/muffinchanel-bracelet-inspiration-pura-vida-inspired-alphabet-bead-bracelets-wax-string-letter-friendship-bracelets-with-seed-beads-4.png?resize=768%2C756&ssl=1" alt="Image of beaded bracelets with letter beads spelling out '❤️', 'LOVE', 'CE❤️', 'ILYSM', 'SASHA', 'BLM', 'HELLO❤️', '❤️PIZZA'" width=500 />
--
All of these are strings:
- `"Hello!"`
- `'I like single-quotes'`
- <code>\`I ❤️ backticks\`</code>
- `'867-5309'`
- `"42"`
--
## `"42"` vs. `42`
*are these the same thing?*
--
## `typeof`
tells you the type of a value
--
Try it!
```js
typeof "42"
```
```js
typeof 42
```
--
## numbers
<img src="https://media3.giphy.com/media/OxeCmRsZsXSCI/giphy.gif?cid=790b7611df6245385829b15db4d7ebd2948ea669e07b5fbb&rid=giphy.gif&ct=g" alt="Gif of Count von Count and the Sesame Street muppets dancing and presenting the number 2" />
--
All of these are numbers:
- `9`
- `525600`
- `-3.45`
- `1.21e9`
- `Infinity`
--
### JS has two kinds of data:
- Primitive types (e.g. strings, numbers)
- Objects (e.g. `document` & friends)
--
## Primitive data types
- `string`
- `number`
- `boolean`
- `undefined`
- `null`
- A couple [others](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values) we don't need yet
--
## booleans
<div style="display:flex">
<img src="https://media0.giphy.com/media/dXFKDUolyLLi8gq6Cl/giphy.gif?cid=790b7611e65175eaf20e344bed912deb1bbe1510918127b6&rid=giphy.gif&ct=g" />
<img src="https://media3.giphy.com/media/98maV70oAqIZtEYqB4/giphy.gif?cid=790b76119164770fd461651da0b494e09dd0e62870f30c3a&rid=giphy.gif&ct=g" />
</div>
--
## `undefined` & `null`
<img src="https://media4.giphy.com/media/RkN1YEwSP4GBvv5bDC/giphy.gif?cid=790b7611f7bab2fa354705ec00978abc19a34b9b64e56d94&rid=giphy.gif&ct=g" alt="Gif of woman gesturing and saying 'blank void of nothingness', from Now Apocalypse on Starz"/>
--
<div style="display:flex;">
<div style="display:flex; flex-direction:column;">
<code>undefined</code>
<img height=250 src="http://campusriot.com/wp-content/uploads/2013/06/I-Want-It-That-Way-Backstreet-Boys-Gif.gif" alt="Gif of Backstreet Boys singing 'Tell my why, Ain't nothin but a mistake...'" />
accidental nothing
</div>
<div style="display:flex; flex-direction:column;">
<code>null</code>
<img height=250 src="https://c.tenor.com/AzSrhbW1nYcAAAAC/tenor.gif" alt="Gif of Backstreet Boys singing 'I want it that way'" />
deliberate nothing
</div>
</div>
--
### Exercise
Which data type is each of these values?
- `false`
- `"true"`
- `document.title`
- `"some string".length`
- `null`
(guess first, then use `typeof` to check!)
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
## Working with Strings
<img width=500 src="https://i0.wp.com/www.muffinchanel.com/wp-content/uploads/2020/09/how-to-make-letter-bead-word-alphabet-bead-bracelets-inspired-by-pura-vida-bracelets-muffinchanel-diy-step-by-step-10.png?w=632&ssl=1" alt="Image of unfinished beaded bracelet spelling 'ALOHA' with text 'Step 13' in corner, via MuffinChanel " />
--
What are strings made of?
![](https://m.media-amazon.com/images/W/WEBP_402378-T1/images/I/61PIxWCLaFL._AC_SY450_.jpg)
<h4 class="fragment">characters</h4>
--
How many characters are in this string?
<pre class="js">
<code>"supercalifragilisticexpialidocious".length</code>
</pre>
```js
"super".length
```
--
Characters are in a specific order
Each gets a number, starting at `0`
<img width=700 height=190 style="object-fit: cover; object-position: 0 -510px;" src="https://i0.wp.com/www.muffinchanel.com/wp-content/uploads/2020/09/how-to-make-letter-bead-word-alphabet-bead-bracelets-inspired-by-pura-vida-bracelets-muffinchanel-diy-step-by-step-7.png?w=631&ssl=1" />
<p class="fragment">aka an "index"</p>
--
What's the first character in the string?
```js
"ALOHA"[0]
```
--
What's the third character in the string?
```js
"ALOHA"[2]
```
--
What's the index of a specific character?
<pre class="fragment"><code>"ALOHA".indexOf("L")</code></pre>
<pre class="fragment"><code>"ALOHA".indexOf("A")</code></pre>
<pre class="fragment"><code>"ALOHA".indexOf("Q")</code></pre>
--
Does this string contain some other string?
<pre class="fragment"><code>"ALOHA".includes("HA")</code></pre>
<pre class="fragment"><code>"ALOHA".includes("LOL")</code></pre>
--
Does this string start with some other string?
<pre class="fragment"><code>"ALOHA".startsWith("AL")</code></pre>
<pre class="fragment"><code>"ALOHA".startsWith("HA")</code></pre>
--
At what index does this substring begin?
<pre class="fragment"><code>"ALOHA".indexOf("HA")</code></pre>
<pre class="fragment"><code>"ALOHA".indexOf("LOL")</code></pre>
--
Connecting strings together
```js
"ALOHA" + "!"
```
--
<div style="display:flex;">
<img src="https://media0.giphy.com/media/3ohhwr2FHK5u2TZFcI/giphy.gif?cid=790b7611b3d751c8682486f8a5140f1fa2d7c44ba387cfb7&rid=giphy.gif&ct=g" alt="Gif from Schitt's Creek of Alexis looking offended asking 'Why are you yelling at me?!'" />
<img src="https://media2.giphy.com/media/UtuJHoRbWW2eb9K8LQ/giphy.gif?cid=ecf05e47hmqd6amklrxj0i3nfbpqlwwpvy7smtfmkqvtzcjw&rid=giphy.gif&ct=g" alt="Gif from Schitt's Creek of Moira looking offended saying 'There's no need to raise your voice'" />
</div>
```js
"ALOHA".toLowerCase()
```
--
### Exercise
Use our new string superpowers to:
- Add your last name in the players listing
- Retrieve the first "T" in the page title
- Answer whether the page title contains the string "JavaScript"
- Capitalize the heading "Tic Tac Toe"
--
There is a lot more we can do with strings...
<img src="https://media2.giphy.com/media/THrVdFFbwhwxNPxfGi/giphy.gif?cid=ecf05e47hxna43czb0oedyxy295x2ywq34xdo4yjvydqhqcr&rid=giphy.gif&ct=g" alt="Gif of Michaela Coel saying 'Sometimes it gets a bit much'" />
--
Remember ✨MDN✨ is your friend!
[JavaScript > Standard built-in objects > String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
## Operators
<img src="https://media4.giphy.com/media/YX6YQZfuRN0nC/giphy.gif?cid=ecf05e47qz4szu7ek9s2zbxa7i4e86beyggj5nwytiq27pr4&rid=giphy.gif&ct=g" alt="Gif of Sade singing 'Smooth'" />
--
We've already seen a couple of operators:
- `"ALOHA" + "!"`
- `typeof "value"`
--
`+` is an operator with several different uses:
<ul>
<li class="fragment">What does it do when operating on strings?</li>
<li class="fragment">Any guess what else it can operate on? </li>
<li class="fragment">How many values does it operate on at a time?</li>
</ul>
--
### Arithmetic operators
- `+` add
- `-` subtract
- `*` multiply
- `/` divide
--
### Order of operations
`4 + 1 * 2 * 4 + 2`
same as in math, use `()` to group
`((4 + 1) * 2 * 4) + 2`
--
## Exercise
Use arithmetic operators to compute:
- The total number of siblings of your parents
- The average number of hours you slept this week
- The number of dogs you'd pet in a week if you pet 1 dog per hour while awake
--
### Comparison operators
- `>` greater than
- `<` less than
- `>=` greater than or equal to
- `<=` less than or equal to
--
`5 > 4`
What type of value will this return?
--
### Equality operators
strict | loosey-<br>goosey | meaning
--- | --- | ---
`===` | `==` | equals
`!==` | `!=` | does not equal
--
Do these differ? How?
strict | vs. | loosey-goosey
--- | --- | ---
`1 === 1` | vs. | `1 == 1`
`"1" === "1"` | vs. | `"1" == "1"`
`1 === "1"` | vs. | `1 == "1"`
<p class="fragment">You should almost always use the <b>strict</b> version</p>
--
### JS has lots of operators!
<p class="fragment">Any guesses what else is out there?</p>
<p class="fragment">Where can you find them all?</p>
--
✨MDN✨
[JavaScript > Expressions & Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators)
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
## Expressions
<img src="https://media.tenor.com/images/6bbe631a75e9da8d1c316bf89ed460cc/tenor.gif" alt="Gif of Madonna dancing & singing 'Express Yourself'" />
--
Are these values?
- `4 / 2 * 10`
- `"Frontend" + "Masters"`
- `5 > 4 !== 3 > 4`
--
an expression *evaluates* (aka *resolves*) to a value
--
<img src="https://occ-0-1722-1723.1.nflxso.net/dnm/api/v6/X194eJsgWBDE2aQbaNdmCXGUP-Y/AAAABVQNgd4IpL64y3okatJlrMQUv0RmPmsyFalQ2aNV5G8SLFI4ghuVYMTV-VTLbHA2G9HQ7Gv1x5RIF7zhGf4Ion3IKDP8.jpg?r=4d2" alt="Image from film 'New York Minute' with Mary Kate and Ashley Olsen looking at each other seated in a cab, with title overlay" />
--
✨MDN✨
[JavaScript > JS Guide > Expressions & Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators)
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
## Variables
<img src="https://www.surlyhorns.com/board/uploads/monthly_2020_08/iu-2.gif.dff1f3975b6ad925cbb783079c18422b.gif" alt="Gif of Sarah McLachlan singing 'I will remember you'" />
--
Variables let us **remember** values
```js
let remember = "Sept. 21";
```
What do you notice about the line of code above?
--
### Declaring a variable
<img src="https://memeguy.com/photos/images/mrw-i-open-my-credit-card-statement-109160.gif" alt="Gif from 'The Office' of Michael Scott yelling 'I declare... bankruptcy!'" />
```js
let bankruptcy;
```
What do you think is the value of `bankruptcy`?
--
### Assigning a variable
```js
let myDeclaredVariable;
myDeclaredVariable = "so value, much wow";
```
--
### Declaring & assigning at once
```js
let myAssignedVariable = "such efficient, amaze";
```
--
### Declaring & assigning forever
`const` declares & assigns a "constant"
aka a variable that can't be changed
```js
const myUnchangeableVariable = "Never gonna give you up";
```
*we'll talk more about this later*
--
### Using a variable
`answerToLife - 10`
`myDeclaredVariable.toUpperCase()`
--
### Variable names
- `validVariable`
- `also_valid_but_less_common`
- `Oddbut_Technicallyfine2`
- ~`0chanceThisWillWork!`~
--
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/CamelCase_new.svg/640px-CamelCase_new.svg.png" alt="Illustration of a camel with the word 'camelCase' positioned so that the captial C is the camel's hump" />
--
## Exercise
Declare & assign variables to remember:
- Your name
- The combined age of your parents
- The `#board` element on the page
--
What really **are** variables though??
--
variables *contain* values?
--
variables ~contain~ *point to* values
--
<img src="https://media2.giphy.com/media/3orieSlpoJxbN34HBK/giphy.gif?cid=790b76110319b1d53fe86c88f59f9ebc32b250b97736c438&rid=giphy.gif&ct=g" alt="Gif from the Simpsons of someone pressing the speed dial button on a phone, captioned '(Dialing)'" />
--
<img src="https://www.akruto.com/images/how-to-save-and-share-iphone-contacts/add-to-contacts-large.png" alt="Screenshot of an iPhone messaging the number '555-555-5555' with the options to 'Call (555) 555-5555', 'FaceTime Audio', 'Send Message', 'Add to Contacts' highlighted in a red box, 'Copy', 'Cancel'" />
--
What happens when this code runs?
```js
let answerToLife = ((4 + 1) * 2 * 4) + 2;
```
--
What happens when this code runs?
```js
let scrub = "guy that thinks he's fly";
let busta = scrub;
scrub = "guy that can't get no love from me";
```
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
## Statements vs. Expressions
<img src="https://media4.giphy.com/media/55GrSbPv8p4pp5E3vC/giphy.gif?cid=790b7611f3e548491596af24d8df5e50a8b3f5baca55fb77&rid=giphy.gif&ct=g" alt="Gif from HBO's 'Euphoria' of Colman Domingo saying 'I can't tell if that's a statement or a question'" />
--
An expression "asks" JS for a value
```
myAssignedVariable
6 + 4
document.getElementById("board")
```
--
A statement "tells" JS to do something
(e.g. declare/assign a variable)
```
let ten = 6 + 4;
myDeclaredVariable = "new value";
let board = document.getElementById("board");
```
--
<p class="r-fit-text">We'll see more kinds of statements later in the course</p>
```js
function add(x, y) {
return x + y;
}
let biggest;
if (5 > 4) {
biggest = 5;
} else {
biggest = 4;
}
for (let character of "string") {
console.log(character);
}
```
</textarea>
</section>
<section data-markdown data-separator-vertical="\n--\n">
<textarea data-template>
## Arrays
<img src="https://media1.giphy.com/media/KMCapxEUpxTMxhnarh/giphy.gif?cid=ecf05e47gqpnohggvkpijhvkurlz3dkeuxvr28gmxvff0es1&rid=giphy.gif&ct=g" alt="Gif from 'Star Trek Discovery' of Michelle Yeoh saying 'You deserve... many things'" />
--
Arrays let us keep multiple values together in a single collection
```js
let synonyms = ["plethora", "array", "cornucopia"];
```
<img src="https://media1.giphy.com/media/QfGYYVrbay6kssMrYn/giphy.gif?cid=790b7611dba71cb23b707b58277a0e27b04c0f48f848072c&rid=giphy.gif&ct=g" alt="Gif of Quinta Brunson gesturing and saying 'Plethora, Array, Cornucopia'" />
--
Like strings, arrays have a `length`
```js
synonyms.length
```
And each value has an index
```js
synonyms[1]
synonyms.indexOf("cornucopia")
```
--
Also like strings, we can check if an array contains a certain value
```js
synonyms.includes("plethora")
synonyms.includes("variety")
```
--
Unlike strings, we can modify arrays
```js
synonyms[1] = "variety";
let lastItem = synonyms.pop();
synonyms.push("multitude");
```
What does each of these lines do? Try it out!