Transcript from the "Arrays" Lesson
>> 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.
>> 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.
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?
>> 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.
>> 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.
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
>> Brian Holt: So that's what arrays for things that you want to keep together that have some sort of order implied with them.
[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.