58 JS Array comma examples - Javascript Tutorial 2018

preview_player
Показать описание
The Full #Javascript #tutorial for #beginners :

Javascript Tutorial 2018

58 comma examples - JavaScript for Beginners.

This is a Javascript tutorial from scratch for any beginner. Use the above link to watch the full JS video tutorial..

Hi, and welcome back.

Let’s look at more comma examples.

If you come here, you see this array begins with this comma.

Now, this means that there’s an element here which is undefined.

Now the element which is here will be undefined and that is the same for this one.

We saw an example of this one in the last video.

So the element which is here is also undefined.

And the one which is here, the one which is inside here is also undefined.

So since this one is here, this and this.

This creates an element in here.

But, there’s no element which is here.

So it’s very important to note if there are 2 trailing commas like in this case, this and this.

There’s an element which is here but there’s none which is here.

So this last comma, the last comma before the final closing bracket means that there’s no element which is here.

So this will just be ignored.

So it’s always important to explicitly define any elements as undefined.

If you’re not going to put an element here, it is advisable to explicitly define it as undefined.

So you can put undefined.

If you don’t want to put any element there, you can just put undefined.

So let’s go to Scratchpad and see an example of this.

Now, if I come back to Scratchpad, this is the example that we did in the last video.

So I’ll just clear out this.

And, I will also clear out these ones.

Now, we say that, so if I look at the length, display the length of this, you’ll see the length is 5.

If I add, so the element which is here is undefined and the element which is here is also undefined.

So the length is currently, the length of this array is currently 5.

If I add that, that means that I’ve added an element here but I have not defined it yet.

So, if I display the length of this array, you see now the length is 6.

And, if I come here and I add one trailing comma, remember, this will be ignored.

So even if I display the length, it will still be 6.

If I add another trailing comma, this means that I’ve added an element here but I have not defined an element here.

So, an element will be added here but this section will be ignore just like a trailing comma is ignored, the last comma is ignored.

So if I display this one, we have 7 elements.

So if I remove one trailing comma out there and I display, you’ll see that it’s now 6 elements.

So the last comma is always ignored.

And as I said, let’s remove that last comma because it may cause issues in certain old browsers.

So as I was saying, if I come to the first element, we want to get that element displayed.

Now, you can see that the element at index zero, this one here is undefined.

So as I said, that is always just best, if you want an element to exist but you don’t want to define it, you can just put it as undefined.

So, if you put them as undefined and you try to get the element in that position, it will bring back that undefined.

So if I try to get the element at zero, display.

It will be undefined.

The one at 1, which is John at index 1.

Display.

That is John.

And then, let me just remove this one.

Remove that bit.

And then the element at 2, display, undefined.

In this video, we have seen some examples of how this extra comma will affect your array elements and the length of your array.

We’ve also said that if you’re going to have an empty element position, you can define it as undefined.

You can have that or you can have undefined.

That’s it for array literals.

And in the next section, we’ll start looking at object literals.

So, I will see you in that section.

GET SOCIAL WITH ME:
Рекомендации по теме
join shbcf.ru