Unlimited Wordpress themes, plugins, graphics & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:18Length:1.8 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.3 Arrays

In this lesson, you’ll learn about the first “composite” data type in JavaScript: arrays.

2.3 Arrays

Hello and welcome to your lesson number four where you'll learn about the first composite data type in Java Script, which is arrays. Now, an array is basically a list of values and the way you can declare one is simply var, let's call it the list equals and we'll use square brackets. And we can put in the list of values. For example I can say John, Marie, and we separate these by commas and Sander. I can think of a better name there. So this is an array which has three string items, but you're not limited to string items you can have a memory of Bullen Zora the Re of numbers already can mix and match. So for example, I can add another one here it says thirty two or I can put another one here that says false. Right. So this is a valid array. Now, this is the first method of declaring an array. You can also declare an array like this, for example, list two equals this, this is an empty array, but it's an array nonetheless or what's called as a list three equals a new array. And here you can also specify its contents, so for example 12 and false. This is a valid syntax. Now this is how you declare an array, but how do you access it? Well, just like you would access a normal variable. So if I go back in here and I say, for example, list, it's gonna give me my array, John Marie Sandard 32 and false, or list two is gonna give me an empty array or a list Three is gonna give me the third array that we've just created. Now this will give you the entire array but what if you want to access a specific element from that array? Well, to do that, you would say list, you open up square brackets and you specify a number from zero to whatever you want basically. Arrays are zero based index which means the first item sits on position zero. The second item sits on position one and so on and so forth. So if I say list zero is gonna give me Jon if I say list two it's gonna give me the third item which is sand or if I say list five it is gonna give me undefined. Now, why is it giving me undefined? Well, because our array has five elements. So its element count only goes up to four. Zero one two three and four. So if I specify an index this is what it's called .If I specify an index that's larger than the the length of the array it's gonna give me on the find but if I see a list four it's going to give me the item on the fifth position. Now ,the way to access these arrays is that the same regardless of how you define them in the first place either using the this syntax or using the new array syntax. Now let's have properties and they have methods, properties or only going to talk about one and that is length, and the length property gives you the number. From that array the way to access that property is with the array name in our case list do List dot length and list both length is going to give me five because I have five items. List to dock length is going to give me zero because it's an empty array and list. Three.length is gonna give me two because we have two items in that array. A very simple property or you can use to find the number of items in an array. Now let's move on to the methods because these are a bit more interesting. The methods we gonna be talking or push, pop, shift, un-shift, and index of. The push method will add an item to the end of an array. So I have my list array here, and if I want to add another item I can just say list.push and then in parentheses I'm gonna put my item and that can be anything. Boolean, number no one string or whatever. So, I'm just going to put a string and I'm going to say hi. So now, it's a gives me the new length of my array and if I do list again I get the updated array. So, that's push. Pop will actually remove one item from the end of the array. So, I can say list the pop make sure you use parentheses because this is a method and that gave me the item that was removed from my list. So if I read you you'll find that we now have an array would just five items the third method is called shift and it's used to removed an item from the front or the start of an array. So if we do a list shift, It removed John. As you gonna see, John is now now gone. Now to add to the front of an array you would use unshift. So list unshift. I'm gonna add ten here. So now list is this bit. Finally index of is used to find the index of a specific item in an array. Lets say for example that I want to find a position of the number 32 in my array so I can do a list, index of 32 and that gives me three right so we have 0,1,2 and 3. What if I do index of this bit and it actually tells me that it's not defined, but if I do index of 3232, it gives me minus 1. And this is the value that's returned in case that item is not found in the array. And that's pretty much it for arrays. Now coming up, we'll talk about the second composite data type, which is objects, see you there.

Back to the top