Tinh common array methods I'm gonna go over some of the most commonly used
array methods in JavaScript the first one is going to be a raid is pushed the
push method this just adds a new element into the end of an array and if I
console that lock that you can see now it's ABCD and you can see that started
off as just ABC the next one I want to show you is a pop pop removes the last
element in an array and returns that element now run that we're gonna see
that we have the D and then if I just cancel that log the array again you will
see that now the array doesn't have that the D on there because it's been popped
off the push and pop changes the original array now next one is concat
which concatenates two arrays first I have to make a new new array so I have
the first array okay all right - if I run that we're gonna
see ABC v/q one thing interesting about that is that it does not affect the
original array so if I just console dot log the second array we'll see that it's
still GQ the next one I'm gonna go over is its join this joins all the elements
in an array to create a string and also does not modify the original array Sofia
run that you can see at the end we just have the string ABC and if we put
something I joined this with just a empty string but I can also put
something in here like an exclamation point I can run them and you can see now
there's an exclamation point between each item in the array and that that
string and the original array stays the same
next up is reverse which just reverses the elements in an array
if I run that you can see at the bottom CBA and that does change the change
their array so if I console dot log the array now you can see at the NCBA CBA
the shift function is going to remove the first element in the array in return
that element so the first Elm is C because we reversed an earlier before we
reverse that array to see ba now it's returning that first element C and if we
just console dot log that array again we will see that it is the C is removed so
if you look at the bottom just gonna be be a for the array I'm shift adds a new
element to the beginning of your right and returns the length of the modified
array so at the end is just gonna say 3 because remember before the array was ba
so this is or added the P to end to make three elements in the array so if we
just log that array we'll see P be a and the P at the beginning of the array
slice is going to select part of an array in return that new already you've
selected in the original array remains unchanged slice is going to accept two
arguments the start and the end minus one
let me give you an example so it's going to start at the index one and then the
second argument is the end - one two - one is me one so this is only gonna be
one character of the array because you always because this it's this minus one
for the end character let me get console that log date so you can see that this
is the original rate pba and we're gonna start at index 1 starts index 0 then we
have index 1 and then we're going to end at the index 2 minus 1 0 1 2 minus 1 so
we can end at the same character that we started at so it's only going to return
B if we change the second argument to 3 then it's going to be PA instead of a
and slice does not change the original ready
so the original array is still gonna be PBA now I'm gonna show you sort was just
sorting array so first I'm gonna push a few more letters onto the array you see
at the end a BF IP so that's all sort of even though you can see that I add F at
the end it ended up sorting everybody it was PBA this is what the array started
off as because this Mike Winston slice is something it's not going to change
the original array so it we had this and we added a F and then it's sorted to be
like that and you can see that sort does change the original array the last one I
want to tell you about is splice splice is very similar to slice but it's going
to modify the original array instead of just returning a new array also the
arguments are going to be a little different
the first argument is going to be the start index also as to the second
argument is the number of items to count from the starting point to remove so if
I put two that means two items are going to be removed
starting at the set the second index I put three then three ends will be
removed and then the third argument is any items you want to add so I'm going
to add Jase nuggets and it's going to return the items that were removed and
then I'm going to log the array so if you can see first it removes fi because
it started off with this array and we went to this index two if we have zero
one two we remove two items one two F&I and that's what it returns for this
first console log and then we're going to console dot log the new array which
is be a be jeaious nuggets because it's going to insert it at the index that we
had up here which is the second index and we're still gonna have the P on here
well thanks for watching my name is Bo Carnes don't forget to
subscribe and remember use your code for good