02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Arrays

Time for learn about data types that have order. so all in order its time to learn about arrays. the quickest explanation of what arrays are is simple an array is a que of data that is ordered from 0 through as many items that are in the list. lets see it in action.

Video Transcript

00:00which this way is called an associative array.
00:03Now, the next type of way,
00:04which is really our next topic of du jour,
00:07the topic of today, is the way of arrays.
00:10The way you access information with an array
00:12is very similar.
00:12So I'm going to create a new array.
00:13I'm going to call it A for array.
00:16And I'm going to say your new array,
00:18uppercase A again, as always.
00:20And now I've created a new array,
00:22which is a type of object.
00:24Now, arrays, contrary to objects,
00:26have a system of counting, which starts from zero.
00:30Thus, currently, our array is empty.
00:33And the way we could find that out,
00:34I could go to that array and ask for its length.
00:37Its length, because it's basically just like a line.
00:39And in our line right now, there's nothing inside.
00:42But if we start sending things into it,
00:44so let's, in our creator, in our constructor,
00:46I'm going to send Ben,
00:47and I'm going to send Sharon,
00:50and I'm going to send my mom, Bill N.
00:53All right.
00:53So here's me, my sister, and my mama.
00:57And if we refresh it again,
00:58now we're going to see that we have
00:59three different things in our element.
01:02But we don't really know how to access it, right?
01:04Because we didn't give a name
01:05to the things that we're accessing.
01:08Well, worry not, because you don't need names.
01:11The idea itself is, when you put stuff into array,
01:13is when things have a relationship with each other.
01:16Such as, what we're going to do in our end exam,
01:19in our exercise, not exam,
01:21is we have your three different prices.
01:23There's a relationship between them.
01:24And what we want to do is just put in array 123.
01:27We don't need to call it specifically.
01:29And then we could run through those three elements
01:32and then put things into them.
01:34All right, so going back into our source code for a second,
01:37and we're going to see how that's done later on,
01:39I want to show you first how you access information.
01:41So beyond our length,
01:42that gives us how much information is within our array,
01:45we could approach our alert,
01:47well, we could approach our A,
01:48and then put inside of it,
01:50in our rectangle bracket,
01:53in our rectangular bracket,
01:55we're going to put inside your numbers.
01:57It starts from zero, and it goes onward.
01:59So zero will be Ben,
02:00one will be Sharon,
02:02two will be Belanne,
02:03and so on and so forth.
02:05So if I run this one more time,
02:07here we go, we're going to see that Ben is in again.
02:09And if we go for our third one,
02:11if we type three, that would be absolutely wrong.
02:13We're not going to get an error message,
02:14we're just going to get undefined.
02:16Why is it undefined?
02:17Because we didn't define anything
02:18inside of the third position.
02:20All right, our second position is our R max,
02:23saying that a lot of people get confused with,
02:25we start at zero,
02:26thus it's not three at the end,
02:27it's actually the length minus one.
02:29So if our length is three, excuse me,
02:33if our length is three,
02:34we would take our length minus one.
02:36So in this scenario,
02:37we would see that Belanne is there,
02:39and if we wanted to,
02:40and Belanne is with two Ls and,
02:43or not, yeah, two Ls.
02:44All right, so, and then we're going to take our,
02:47just for an example,
02:48I'm going to take the length of our array,
02:49minus one right inside of it,
02:51just to show you the exact meaning of it.
02:53So this is the max, the extreme scenario.
02:55And it would be the exact same thing
02:57as what we've done just a second ago.
02:59Okay, so we've seen how to add stuff into array
03:02directly immediately when we created an array.
03:04What if we wanted to create more stuff
03:06or add more stuff later on?
03:07There's a few ways of doing it.
03:09One way is just approaching directly and changing.
03:12So for example, let's say I want to get rid of Ben,
03:14which I hope you don't want to get rid of me.
03:16But if you do want to get rid of me,
03:17you just go to, you know,
03:20the thing that is in position zero,
03:22and let's say I want to replace it with something else.
03:24Let's say I want to replace it with Danny.
03:28All right, so now we replaced Ben with Danny.
03:30And if I go into our alert box here,
03:33and I'll just push it underneath,
03:35and we're going to call position zero,
03:36we're going to see that Danny's there,
03:38Ben is not there anymore.
03:42Perfecto.
03:43Now, I just want to give you just a fun, quick example
03:46of what would happen if you want to switch
03:47between Ben and Sharon?
03:49What if I wanted,
03:50and we don't know that Ben or Sharon is there,
03:52we just want to switch between position zero
03:54and position one.
03:55This is just a fun exercise and a reminder
03:57of different variable types.
03:59So for that, I'll create a temporary variable
04:01and I'll call it temp name.
04:04And I'm going to put into temp name
04:06the element that's in position zero.
04:09Now that I have access to the element
04:12that's in position zero,
04:13I could go to position one,
04:15and say put into position,
04:17well, I could go into position zero actually,
04:20and say what we'll put into position one now is one.
04:24Now what do we want to do with position one?
04:26We want to put inside of it our temp name.
04:28Right, so this is just saying a cute small,
04:30I don't know if cute,
04:31but saying to notice that
04:33we couldn't just automatically switch it
04:34because we would lose a reference,
04:36we had to create another variable
04:38to enable us to switch between their positions.
04:40Obviously, usually we would do it inside of a function,
04:43that way automatically would get rid
04:44of the function temp name,
04:46which is not needed anymore as soon as we've done it.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!