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

Starting Up With Content

Creating an array that contains initial elements.

Video Transcript

00:00Let's look at one more option of how we could create an array.
00:03If we wanted to create an array and we already knew in advance the data that we want to put into it.
00:08Let's say for example we want to put animals.
00:12And for some reason it's really important for us that the order of the animals are going to be in a certain order.
00:18So let's see how we would do that. If I open a new array,
00:21I'm going to open a bracket and instead of telling our array how many items it would have,
00:26we could just put into it immediately our items.
00:30So for example we could put inside of here a cat and then a dog and then a bird and then an ant.
00:41And we've created a new array. Now if we trace out our new array,
00:46it should be smart enough and it is and it tells us how many items we have in our array.
00:54Now in the same way that we have approached our array to add items into it,
00:59we could approach our array as well to retrieve data.
01:02So if I wanted to retrieve the data that is in position zero for example,
01:08and I would run it, I would be able to fetch my cat data.
01:12And same is true if I wanted to fetch everything.
01:15So if I wanted, I know we have only four items,
01:17so I could just trace out four times position zero, one, two, three.
01:22Position zero would give me my cat, position one would give me the dog,
01:26two, the bird, three, the ant.
01:29So if I run this one more time, we should be seeing all the items and then their length.
01:34So this is really, really in a nutshell the really, really simplistic way of working with arrays.
01:40Now with that said, before I close down this video and we move on to actually working with array methods,
01:46let's just highlight one more item here.
01:49Nothing inside of the array is constant.
01:51We could always change it.
01:52We could always change our mind and go to position two and say from now on,
01:56you're not going to be a dog, you're going to be a window.
01:59And then if we trace out our position two again,
02:04we will see that our dog has been replaced with a window,
02:11or more accurately actually it's zero, one, two.
02:13So our bird has been replaced with a window.

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!