Loading
Current section: 2. Arrays 5 exercises
solution

Array Basics

Transcript

00:00 Alright. Let's just go get on into it. So we're gonna start with, creating an array of product names. So laptop, mouse, keyboard, that all all works. And then we wanna add a new, item to that array.

00:13 So we're gonna push that, a monitor onto that array. And before we we get too far into this, just to call out the syntax again here, it's a open bracket. This is, as an expression. So anywhere you can put an expression, you can put an array. Open bracket, and then you have your, items in the array.

00:33 So in this case, it's a string, but you could do a number. You could do a boolean. You can do whatever you want to in here. Any other expression. And so we've got the expression laptop, mouse, and, keyboard each separated by a comma.

00:47 And then you can add a new product using, this push method or, function on that product array. So it's it's an object. That's why we're accessing the property in the object with the dot and then you have push. So that's, one of the really interesting and and, things you need to keep in mind with in JavaScript is that pretty much everything is an object and so you're gonna access properties and things off of those using the dot. So even an array actually is an object.

01:17 So we're gonna push, monitor here and then we want to log the first item. To access an item in an array, you use the brackets that we use with objects. You cannot do a dot because that's syntactically is just not allowed. And so, when you're accessing items, it's always gonna be with bracket and then the index. And the funny thing about JavaScript eraser or eraser in other languages as well is we like to be base or index zero.

01:45 So the first item is the the zeroth item. It's at index zero. And as a result, a lot of errors in programming tend to be off by one errors because we start at item If you have three items in your array, you will have the last item at index two, which is kinda off by one. And so it's easy to miss and mix up. Where Yeah.

02:10 We can access the last product with products dot length. So that's the total length of the array minus one because we're index, starting at zero. So we're gonna access the last product. We're gonna get the total length of products and then we can just console log the products themselves. And we'll save that.

02:28 And we've got our laptop. Was that our first product? Yes. It is. The monitor, that was our last product.

02:34 We added that one. And then we can look at them all. Laptop, mouse, keyboard, and monitor. There you go. I wanted to mention one other thing and there's actually another way that you can access properties and that is with a special method called at.

02:46 So I'm gonna stick this right here with console dot log products at and you can do at whatever index you want to. So at zero, so that's basically the same as this. So we got two laptops going on here. You can, of course, do at one. But the cool thing about at is that you can actually go, you can use minus.

03:09 So if I do minus one, then that's gonna get me the last one. If I go minus two, that'll be the second to the last one. So that's kind of useful. So the at method is, yeah. Definitely something that I wish I had when I was learning JavaScript, at the beginning, but this is actually a relatively new feature in JavaScript.

03:29 You can also I'm I'm doing control space to pop this open. Every editor is gonna be a little bit different. But, yeah, this is going to show us all of the methods that are available on the array object and there's a bunch. You can do all kinds of things with arrays. So feel free to dive into some of those.

03:45 There's a couple interesting ones. You can find the index of a particular item and you can though there yeah. Index of, so yeah. If you have an item, you can look up, its index, last index of. So start at the end and look for, the most the last index of that thing.

04:06 Lots and lots of cool things. So feel free to explore arrays. They're pretty neat. Let's keep moving.