declare

12: How to Create Arrays in JavaScript | Store Multiple Data in an Array | JavaScript Tutorial



Sharing buttons:

how's it going on wanna come back to our

next episode on how to create JavaScript

now in this lesson you're gonna learn

about something called an array and an

array is very similar to when you

learned about something called the

variable some episodes ago now just to

recap a variable is a way for us to

store information inside a container and

an array on the other hand is a way for

us to store many pieces of information

inside one container so instead of

having you know one container with just

one item inside of it we can have one

container with many pieces of

information stuffed into it so we can

use it for a later purpose so the way we

create an array is very similar to

include variables so in this example

here we'll have nothing inside my script

tags we can go ahead and create a

variable by declaring a variable and

then we'll call it some kind of name

like items just to give it something and

usually with a variable with just that

are equal to and then something like a

string or a number or something but in

this case since we have many pieces of

information needs to go inside this

container I'm going to create a pair of

brackets now inside the brackets we can

go and create all a different lists of

data that we need inside this specific

array here and when we create these

brackets we automatically create an

array instead of a variable so what I

can do is I can say we have for example

a string called bottle then I'm going to

create a second string call something

like camera and just to do something

more we can create a third string and

just go and call this one juice just to

have something so right now we have an

array with three different pieces of

data inside of it

now this is one way we can declare a

variable and I'm not going to confuse

you with a bunch of different ways but

just to show you there's another way we

could have potentially written this out

depending on the purpose later on when

we start learning about loops and that

sort of thing that we might want to do

it in a slightly different way so

another way we could do it is just

declaring the variable so we're

declaring again the same variable called

items and I'm just going to close it so

we just declared it but we haven't done

anything to it then later on we can go

ahead and say well we have this variable

called items that I want to set equal to

whatever we did up here so I'm just

going to copy it

and paste it down so this does the exact

same thing as that Bob we just declared

it first and then we created the values

for the variable afterwards creating an

array instead now just to be clear the

way that we usually do it when we just

need to create an array is the first

method up here it's not till when you

start talking about loops and that sort

of thing inside JavaScript that you need

to worry about creating a array this way

instead of the above now when we create

arrays inside JavaScript we don't have

to just use strings like it in this case

here you can use any kind of data we

have inside JavaScript in order to

include inside an array so right now I

could for example say instead of camera

we could say we have a number called 4

and instead of Jews we could create a

boolean which we also talked about in

the data types episode called true so

you can create any kind of data inside

these arrays we can even have a race

within a race but that's not something

we're gonna talk about in this episode

we'll talk about that when we get to a

later episode so now we created this

array here so now the next question is

how do we actually get the data and show

it inside a website so what we can do is

I can go down to next line and I can go

out and say we have a console.log to

give an example because that is a way we

can get data out on the website inside

the console I can go inside the

parentheses and then when I want to

reference to a piece of data inside the

array I just write the name of the array

brackets and then I need to refer to the

index of the data inside the array now

the index is the precision of the data

inside the array so if you were to take

a look at this array up here you can see

the first data we have inside of it is

the one called bottle which is a string

the second piece of data is number four

and the third piece of data is true

which is a boolean now when I refer to

the first data inside the array we need

to use index number zero because when it

comes to JavaScript or any sort of

programming language we typically start

at the number zero and then one two

three four and so on so on so if I want

to refer to the first index which is

bottle then I write zero inside the

brackets here then if we were to go

inside my website refresh you can see we

get bottle inside the console if you

want to refer to the second data we say

one and then when I go inside the

website refresh you can see we get four

so this is how we refer to the data and

spit it out inside our website now the

last thing I want to talk about when it

comes to arrays is that we can reassign

values inside the array so right now we

have the items array if I want to change

for to for example eight then I can go

right after we declared the array before

we actually control lock it and I can

say items brackets and then refer to the

index of the number four which is one

because remember we start at 0 so 0 1 2

and then I'm going to set it equal to

something else we can also change it

into a string or whatever we want to do

but let's just go and change it into 8

and then if it were 2 console.log and

inside the website you can see that we

now get 8 inside the console so this is

a way that we can redo or reassign a new

value inside an array so this is what an

array is and this is something we've

used quite often when we create Java

Script so when we get to a couple of

projects or not so long time and then

we'll be creating something using a race

as well so I hope you enjoyed I'll see

you next time

[Music]