#100DaysOfWeb in Python Transcripts
Chapter: Days 25-28: JavaScript Introduction
Lecture: Arrays

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's look at JavaScript arrays
0:04 which will be the equivalent of Python lists.
0:13 Defining one is very easy, and similar to Python.
0:25 An array has a bunch of methods we can call on it
0:29 and see, by the way, the nice console feature
0:32 of autocomplete
0:35 and showing the output already before typing till the end.
0:42 I can sort the array. I can reverse it
0:49 and notice that those operations are in place
0:53 so the list is now reversed.
0:57 The slicing syntax is not as powerful as Python.
1:01 You can do it for the first item
1:05 but not so for the last
1:07 let alone taking a slice.
1:10 So you have to use methods to accomplish the same.
1:18 Sorry, it's not itertools.
1:20 To add items to the array you can use names.push..
1:27 It prints to new length.
1:31 You can get the last item using pop, just like Python.
1:40 And to get the first item, you can use shift.
1:46 And again, those operations are in place.
1:53 Those cover the main use cases
1:55 of working with arrays in Javascript.
1:58 And to recap, defining an array, very simple
2:01 same syntax as Python.
2:06 You can then get the first item
2:08 you cannot get the last item like this
2:11 let alone slicing.
2:14 But you can use the slice method on the array object.
2:23 We can reverse an array in place
2:26 sort it, get the last item, get the first item
2:34 add a new item or name and get the length.
2:44 So we still got names. Let's add one more.
2:52 And another one. So we got four names.
2:56 And the classic way to loop through an array
2:59 is very C-like by doing a classic for loop.
3:06 And a local variable of i, the index, we set to zero.
3:11 We go till i reaches the length of the list.
3:18 Really like this autocomplete.
3:21 And let's say we just print them out, right?
3:25 And we have to index in the list.
3:31 And make it small for
3:33 and by the way when I was talking about indexing
3:36 I only showed how to get the first item.
3:39 But you can of course get the second item
3:41 the third item up until the fourth item.
3:44 And there's not a fifth item.
3:47 And that's interesting
3:48 because Python would actually crash here
3:50 and Javascript again is very permissive.
3:53 It's not raising an exception, so be careful.
3:56 So this is the classic for loop I showed.
4:00 It's quite a lot of code so starting ECMAScript 2015
4:04 there's a shorter, more concise way to do this.
4:07 So you can do
4:09 for { console.log ; }
4:17 so for { console.log ; }
4:21 and it gives them all four.
4:22 And that's a bit less code to type and it's more elegant.
4:28 So again we have the C-style for loop
4:33 with indexing into the array
4:36 but starting ECMAScript 2015
4:39 you can actually write it more eloquently
4:42 and both for loops print the names
4:45 in the order that they're in array.
4:47 In the next video we're going to look at conditionals.