[Music]
welcome back aliens my name is david
reddy and let's continue with this
series on javascript
in this video we'll talk about arrays
see in the earlier video we have talked
about
objects right and then before that we
have also talked about primitive types
and then we have also discussed that
whatever data is not a type of primitive
it will be an object
now this can be the object which we
created then we have functions which are
objects
and then we have areas which are objects
but then when you have an object
why you need another concept like arrays
so if you look at the
object we have a key and a value right
example when you talked about
aliens or laptop it had a key and a
value pair
example for laptop it can be cpu which
is your key and then i9
i7 those are your values maybe you can
go with another property which is let's
say ram
which is your key and then let's say 16
gb is the value but in this case let's
say you want to have multiple values and
you don't need to have a key
so let's say you have a lot of values
and then you don't want to assign a key
to it
maybe you want to work with the index
values can we do it now if you're coming
from other languages like cc plus with
java most of the languages has this
concept but if you're new don't worry
whenever you have a collection of values
which you don't want to assign a key to
it maybe you can
use numbers to represent them we can use
a concept like arrays how do we create
an array here
so let's say i want to create a array of
values so what we can do is to create an
array we can say new
edit this is one of the syntax which we
can follow so we can select
values equal to new array and now you
can guess this is a constructor function
right because we are using a new keyword
so basically we are trying to create an
object okay there is one way of creating
an array the other way is you can
directly assign a value
here itself maybe you can give a square
bracket and you can assign the values
okay so this works in the same way the
other method works so we'll go with that
only
but again you can replace that with the
new array keyboard here so let's use
the empty square package this is another
way of getting an array now this
is your empty array so what we have done
earlier with the help of new array is
same as this one
both will give you an empty array okay
so when you empty it will not have any
values right how do we check that so
it's very simple you can just print the
values to verify right
so we can simply come back here and we
can say values and when you run this
code of course it will not print any
values because it's blank so yes it is
printing it's an array but we don't have
any values now how do we assign a value
of course there are multiple ways of
doing it so what if you know the values
already so in that case let's say the
values are 5 7 and 8 that's how you
assign the values so you can say 5 comma
7 comma 8
and you got an array with these three
values of course there is no compulsion
that they should only have numbers you
can also have string
or you can have other objects you can
have a function your choice
let's print this and you can see we got
an array with three values
that sounds cool right okay can we just
print the length so maybe i want to know
the length of this array so that's
simple you can simply come back here and
say length so values.length and it will
print the length of the array and the
length is three
okay so you have three values in this
array quite simple right so this is how
you create an array the same thing you
can do with new ad
it's just that new array will give you
empty array and you can assign the
values later
but how how do we assign the values
later so let's say i want
empty adder here and i want to assign
values
in that case you can use certain
functions maybe you can use
a function like push or you can say
methods because this thing actually
belongs to an object right
so i can say values dot i can say a
method which is push
now what this push will do is whatever
value you will assign here let's say
5 or this value 5 will be assigned
in this array okay so when you say push
it will add the element
let's see if that works uh let me just
remove this length i want to print the
actual values and let's see what happens
ah so you can see we got only one value
now which is five so initially it was an
empty array
but then you said hey i want to add one
value it's doing that
what if you repeat this what if you just
copy this and
add another value let's say seven and
what will happen now
so it will have five and seven and
likewise if you add some more values it
will
add that as well okay uh so that's how
you can push the values
but again i will see this later let me
just go with the predefined values here
which is five seven and eight okay so
when you print
the values it's quite simple right it
will simply print all the values but
what if
you want to fetch one particular value
let's say i want to
print the first value the second value
third value how do we do that
now we have seen this in objects right
in object what we do is if you want to
fetch a particular value
you ask for the key right so you
mentioned hey we have this value
i want to fetch that with the help of a
key but in this case we don't have a key
right we all have values here but then
as i mentioned before
maybe you can fetch this with the help
of numbers something like index numbers
so this 5 will have a number this 7 will
have some index number
8 will have some index number example
you know when you create a table
you have a column which is the first
column right serial numbers
right uh example the number of the line
number here so these are the index
numbers you can imagine
uh but for this value of phi what should
be the index value so what i will do is
uh first of all how do you measure index
value
uh the same way you whenever you mention
array it always represents with the help
of
a square bracket so you are saying hey i
want to fetch values
and i don't want to fetch all the values
i want to fetch the values
with the index number so i can say hey i
want to fetch the first one
so i will say one and let's see what
happens so when you mention
one and let's run this code oh oh we got
seven we wanted five but we got seven
now what went wrong here is
uh in computers basically whenever you
talk about index values
it always starts with zero so this five
here it
should be zero the index number is zero
here okay so we should start with zero
so when you say five is zero then seven
will be one
and eight as is at location two okay
let's run this and you can see we got
five
okay uh how about if i say one uh it's
quite simple now you know it should be
seven we have done that before and then
when you say two it will print eight
let's try two as well
and let's run this code and you can see
we got eight but what will happen if i
go for three because we have only three
values
and when you say i want to fetch the
element at index number three that means
we are trying to fetch the fourth value
we don't have a fourth value here what
will happen you will get undefined so
luckily you don't get others
in fact i am not a big fan of undefined
i believe we should get either that
there is no element outside three or
maybe you can you should get
something like in java you get other
index sort of bound but here we don't
have that you know
it's printing undefined uh but dad you
have to remember this whenever you have
the add a if you want to fetch a value
maybe you can
first check what's the length of the
array and then you can fetch the value
yeah so that's how you can create an
array and that's how you can fetch a
particular value
how do we add elements with the help of
index value that will say in the
next video so i hope you are enjoying
this video let me in the comment section
and do subscribe for the videos bye