Transcript from the "Arrays" Lesson
[00:00:00]
>> Brian Holt: Well, let's talk about arrays now. So an array, you'll often hear me refer to them as lists as well. For the sake of JavaScript, those are synonymous terms, link arrays and lists. So yeah, if I say that just realize that's what I actually mean, but they are called arrays in JavaScript.
[00:00:22]
In other languages, there are multiple types of arrays. In JavaScript, there is just the array. There's nothing else.
>> Brian Holt: So something that bears mentioning about objects is they are unordered, right? So despite the fact up here that I declared my object name first, then city, then state. I heard there is no order implied there, right?
[00:00:45]
So if I asked for the spec, in fact, notice it gives it back to me in a different order. I declared name first, but it gives me back city first. I think it's just alphabetizing it, right? Yeah, it alphabetized it here. So you have no guarantee of order with objects.
[00:01:05]
>> Speaker 2: Can you force an order?
>> Brian Holt: No, you can't. That's part of the specs of objects. Cuz it's like if I put a bunch of marbles into a bag and I reach out and pull marbles out of the bag, I have no guarantee of what order I'm gonna get the marbles back out it.
[00:01:20]
It's kind of the same concept. Your putting properties into a property bag and then your pulling them out, but that's the point of arrays. Arrays are ordered, yeah.
>> Speaker 2: Wait, so how do you determine if it's an array or an object? Is it just between curly brackets and square brackets?
[00:01:37]
>> Brian Holt: Cool.
>> Speaker 2: You crack the code.
>> Brian Holt: [LAUGH]
>> Brian Holt: So objects are used for things that don't necessarily have order, right? Like for example, the properties of a person don't necessarily have an order, right? That doesn't mean like should their address first come first or their name come first, but there's not really an implied order there, nor is it necessarily important.
[00:02:01]
However, if I'm talking about days of the week, there is an order there that needs to be preserved, right, and that's what I raise her for. So in this particular case, the order that I put them in there is the order that they're going to stay. So if I console.log(daysOfTheWeek), notice they come back in precisely in the same order.
[00:02:24]
And if I asked for days of the week zero arrays started zero, this is very, very important for you to remember. I will probably say like seven more times a raise started at zero. So the first element of an array is index zero, okay? So if I asked for the first element of the re index zero, I get Monday, right, which is the first thing that I put in.
[00:02:47]
Yeah.
>> Speaker 3: That's a constant in JavaScript though, that it always starts at zero, right?
>> Brian Holt: Yeah, everything starts. For example, those strings that we were just working on, right, everything starts at zero and that's pretty much every programming language. There's like some old language that I can't remember which one it is.
[00:03:05]
It starts at one and it was wrong.
>> Speaker 3: [LAUGH]
>> Brian Holt: Everything starts at zero in programming. So this is the preferred way here where you use the square brackets afterwards to access particular properties in those arrays, right? So I put 0 in between the square brackets.
>> Brian Holt: I'm not mistaken it should work with a dot, as well
[00:03:30]
>> Brian Holt: So that's what arrays for things that you want to keep together that have some sort of order implied with them.
>> Brian Holt: Notice that these are all strings. In JavaScript, they don't necessarily have to be a homogeneous type. It doesn't necessarily have to be all strings are all numbers are all objects or anything like that.
[00:03:49]
However, I'm just going to admonish you that it will help if they are all the same things. Because it's just eliminate entire classes of bugs, right, if you keep it them to be the same types like it would be all strings or all the same types of objects.