Okay, I can also create arrays using a short hand array imitation.
So let me go ahead and comment this out so it doesn't crowd our console.
So we'll save it and we'll clear the console.
Let's go ahead and say, short hand array creation.
Okay, so what we're going to do is say, var names, and
we're going to create an array simply by providing an empty brackets.
So empty brackets now creates an array in the variable names, and
what I could do is, I could start listing my names, Yaakov, John, Joe, let's say.
Okay, so if I do console.log now, on the names,
I should see that array right there, Yaakov, John, and Joe.
So that works, great.
So now you see that there's two ways to create an array.
One is kind of a long hand notation where you say new array, and
the other one simply by kind of like an array literal something like that,
but we just use the brackets and you separate the array values by a comma.
And again I could put whatever I want in here, so if I wanted to let's say, I can
actually use spacing here like this and if I wanted to I could create an object here.
So let's see, I can create an object and say, name: "Yaakov", and comma, there's
another object here if I wanted to have an object, we'll do name: "John" and I
can actually leave it here because arrays are not restricted to have the same type.
So if I save that and I'll see object, object Joe, right?
So if I actually opened that up, it will let me open this up and
we'll see Yaakov, name Yaakov, and the same one is second one is named John.
Okay, but let's undo that for
now and go back to the regular array we had before, and let's go ahead and
loop over this array printing out each name, let's say, say hello to each name.
So for that we're going to use a regular for loop.
We'll say, var i = zero, and
we need to keep looping until it's the end of the array.
Well, there's three items in the array and
each array object has a special property called length.
And it's not a function,
it's just a property that the JavaScript engine sets for you.
So array names that length, we'll give me the length of the array,
in this case it will be three.
So what I'm saying is that I want my loop variable to be zero, one and two and
when it's three it's no longer going to be less than the length of the array and
it will quit.
Okay, so let's go ahead and print hello to the console.
So we'll say console.log and we'll say, "Hello" space,
and then we'll say names[i].
So now every time through the loop the i is going to change and
it's going to say hello to a different name.
Let's go ahead and maybe comment that out so we don't see that on the console, and
we save it and we say hello Yaakov, hello John, hello Joe.
Now what's interesting about arrays in JavaScript is that they can even
be sparse.
So I'm going to go ahead and make some room here and I can say something like
names and I can say name[100] and I could set that value to be let's say Jim.
So now we have an array that has zero, one and
two index and an 100 index and in between there's nothing there.
So if I actually go ahead and take that here, and paste that loop right here and
execute it, what we're going to see is it said, hello Yaakov, hello John, hello Joe.
And it starts that again, hello Yaakov, hello Joe and then 97 times it just says
hello undefined because what we're doing here is we're saying I want you to
retrieve names with an index of four, and five, and six, and so on.
Those indices just don't exist in this particular array, but 100 does exist,
so therefore you see when it gets to 100, it prints out hello Jim.
There is one more issue with arrays that you should be aware of, and
we'll discuss that issue in part two of this lecture.