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

Talk Python's Mastodon Michael Kennedy's Mastodon