Lesson 20
Introduction to JavaScript Arrays
Summary
Summary of JavaScript Arrays Lesson
Welcome to MBK Tech World! This lesson focuses on arrays in JavaScript, which are ordered lists of items.
Key Points
-
Definition of an Array:
- An array groups multiple items (e.g., names) into a single variable.
- Unlike defining multiple variables (like
name1
,name2
,name3
), an array allows for easier organization.
-
Creating an Array:
- You can create an array using square brackets and separating items with commas.
- Example:
let names = ["John", "Anna", "Andrew"];
-
Array Properties:
- The array is treated as a single variable containing multiple items.
- The number of items in the array (elements) can be accessed using the length property.
- The array's elements can be accessed by their index (starting from 0).
-
Viewing Array Contents:
- In a browser console (e.g., Google Chrome), typing the variable name reveals its contents.
- Elements in the array can be viewed alongside their positions:
- Position 0: John
- Position 1: Anna
- Position 2: Andrew
In summary, arrays simplify the management of collections of related data, making it easier to store and access multiple items efficiently in JavaScript.
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: