Lesson 20
Introduction to JavaScript Arrays
Video Transcript
Welcome to MBK Tech World. This lesson will be about JavaScript arrays. An array is an
order list of items. So let's start off with an example. Remember that before when we first
learned about variables, let's say you un-store somebody's name like this, right? But now,
suppose you not only have one name, but you have multiple names. Say we have three people.
We could do like so. We could call this variable name one. We could make another variable for
the other person, let's say Anna. And then yet another variable for another third person,
I don't know, like Andrew. And then we could access each of their names using name one,
name two, name three. But notice this is kind of tedious to have to define each variable
one by one. So isn't there a better option we can do? Yes. So that's when arrays come
about. So you can group a list of items, like in this case, I have names, right? String,
three strings, we can group them in a single entity, a single variable. And we call that
an array. Simply put, it's just a list of items. So instead of doing one variable at
a time, you only create one variable. For example, let's create a variable called names.
And this variable will hold a list of names. Okay. So all you have to do is you separate
them with a comma. So let's put John, Anna, and Andrew. And then you put square brackets
around this list. Okay, the square brackets means, okay, we're making an array, we're
making a list of items separated by a comma. This extra space here is optional. It's a
style convention that I use, like all the other style conventions that I already mentioned
to you. Okay, so it's just a normal variable, except now we're not only starting just one
single strings, but a list of three strings. And this list is called an array. When you
see square brackets, think about array, okay, in this context. So let's define it. And now
let's see what's inside. Let's just type names. And you can see there's a number three, and
then you have the list right here. So three means, there are three things inside this list.
Now those things have a name. And the formal name is an element. So an array has elements.
So each one of this, these guys is called an element. So this array has three elements.
And if you click here, I'm using Google Chrome, by the way, in this video, you can expand
this and Google Chrome allows you to see what's inside this list. You see at position zero,
there's John, one, there's Anna, there's two, two, there's Andrew. So there's an order to
the array. So the order that you define will also be appear in the same order here when
you try to access them.
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: