# JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)

## Метаданные

- **Канал:** Corey Schafer
- **YouTube:** https://www.youtube.com/watch?v=nAWVYFEzoY8
- **Дата:** 18.03.2015
- **Длительность:** 10:31
- **Просмотры:** 10,216

## Описание

JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)

In this series, we will take an in-depth look at JavaScript Arrays and everything we can do with them.

In part 2, we will go over: toString(), reverse(), concat(), and join()

Part 1: https://youtu.be/8JgU2WmrZXI
...
Part 3: https://youtu.be/cdPS-lmlwco
Part 4: https://youtu.be/JskeRdu_X8Q
Part 5: https://youtu.be/w4KF_lapbRI
Part 6: https://youtu.be/1gupsllu5wQ
Part 7: https://youtu.be/qxzp4X6sfGo


✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1

✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer

▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter - https://twitter.com/CoreyMSchafer
Instagram - https://www.instagram.com/coreymschafer/

## Содержание

### [0:00](https://www.youtube.com/watch?v=nAWVYFEzoY8) ToString

now just like it sounds the tostring method on an array will return a string representation of that array so if i un comment out my code here then i have this array that is just a string that says this and then a number 1 and then a string that says time and if i run array 6 dot 2 string and then i output that string to the screen then you can see that it returned this string that just has commas and you can use this variable throughout your code this is usually used just for you know maybe logging out an array to the console or something like that so that is the tostring method now let's take a look at the for each method

### [0:53](https://www.youtube.com/watch?v=nAWVYFEzoY8&t=53s) ForEach

method the for each method will execute a given function for each element in the array so let me go ahead and uncomment out these lines here and you can see that we just have a test array which is one two three four five six seven eight nine and then i'm outputting that to the screen here so you can see it a little bit better um so now let's go ahead and write that callback function that we're going to use with our for each method so i'm just going to call this function times 10 and now this callback function has access to a few different parameters here one of those is the element and that is the current element as it's going through the for each method this is the current element in the array and then we have access to the index which is the index of that element and then we have access to the entire array the entire original array so let's just look at an example as to what it would look like to use all of these so i can type in array which is the original array and then i'm going to access the in the current index of that array and then i'm going to set that current index equal to element times 10. and now let's go ahead and run this for each function and then i'll kind of explain a little bit better as to what it did so let's do array 7 dot 4 each and then we'll use that times 10 function and then let me output that to the screen okay and now you can see that our original was one two three four five six seven eight nine and then after we ran our four weeks function now the array is 10 20 30 40 50 60 70 80 90. so what happened here was that when we ran this for each method it runs times 10 for every element in the array so it goes through so say for example we're looking at our first element here one so this element would equal one the index would be zero and the array equals the original array so we're accessing the zeroth index of the original array and setting that equal to the element which is 1 times 10. so that's what you get there and then it just goes to the next spot so and then the next element's 2 and the index is 1 and with the current array and then it goes through the entire array like that and runs that function for every element in the array let's

### [3:35](https://www.youtube.com/watch?v=nAWVYFEzoY8&t=215s) ForEach Example

take a look at a slightly more advanced example i'm going to go ahead and comment out that first example and then uncomment this code here okay so we have an array of objects and all these objects are three people with a name and an age you can see here i've already written out uh the function that i'm going to use for this uh for the for each method and what this does is it just outputs each of these people in the html so you can see if we go in here it will print out person is equal to the index plus one so for the zeroth index that would be one and then for the next is two nexus three and then it uh prints out their name and then it prints out their age you can also see here that i'm not using uh the array parameter anywhere in here so if you're not using one of these and the callback functions you can actually just go ahead and emit it and it won't have any effect on the end result so that is the for each method as you can see it's a little similar to a for loop but it's a little bit more elegant and you know you don't have to worry about things like out of bounds exceptions and things like that so let's go ahead and move on to the reverse method

### [5:04](https://www.youtube.com/watch?v=nAWVYFEzoY8&t=304s) Reverse Method

reverse is pretty self-explanatory if i uncomment out my code here you can see i have a test array that is just 0 through 9 and then i'm outputting this original array to the screen here and if i want to reverse this array all i have to do is do that array and then call the reverse method save that and then i'll output that to the screen and you can see that the reversed version of this array is nine through zero and just so you can see what this looks like on a small array of strings let me go ahead and comment that out and uncomment that and you can see here that this original array is reversed this array and then you can see that array reversed here with the reverse method so that is the reverse method for the array next we're going to take a look at the concat method

### [6:05](https://www.youtube.com/watch?v=nAWVYFEzoY8&t=365s) Concat Method

will take an original array and it will concatenate it with other values or other arrays so for example let me uncomment out my code here and you can see here i have two arrays one is equal to abc and the other is equal to def and i've outputted both of these to the html here so if i was to take this variable a rake and cat and i was to set this equal to array 91 which is my abc array and run concat and then array 92 and save that and then i'll output that to the screen and you can see here that this concatenate is equal to both of these arrays concatenated into one a b c d e f and what this result would look like in code is one big array that has taken all those values and put them into one single array so

### [7:11](https://www.youtube.com/watch?v=nAWVYFEzoY8&t=431s) Mixed Concat

you don't only need to send in arrays as parameters for concat you can also use some mixed parameters so you can see for example here i already have this example typed out this array concat 2 is equal to our abc array concatenated with just some random values thrown in here i have the numbers 1 2 3 and then for my fourth parameter i put in an array of d e and f and you can see whenever i output this to the screen it still takes all those values and concatenates it into one single array so in javascript if you see here in this comment uh what this would look like in javascript would be one big array that just takes all these values and puts them into one single array like that so that is the concat method now we are going to take a look at the join method joins

### [8:18](https://www.youtube.com/watch?v=nAWVYFEzoY8&t=498s) Join

join method the join method joins elements of an array into a single string and it does that with a separator that you can specify so if i uncomma uncomment out my code here we can see that i have a sample array right here that is just uh 192 168 1 and 1. and if i wanted to join this into a string then i could set this string 10 equal to array 10 join and if i just ran that without any parameters then you can see that by default it makes a string with the default separator as a comma but i can pass in anything that i want here i could do if i did a period there then you can see that now i have 192. 168. 1. 1 let me show you a couple of other examples using an array of strings i could join this array together and you can put in a space to where it looks more like a sentence or you can put in dashes or anything that you want the separator to be now if you have an array of characters like the third array example here then what you can do is you can actually take out everything and just do two quotes with no spaces in between and instead of doing the default separator which is a comma it will just bunch all those together and whenever it joins these together into a string you can see it takes these array of characters and puts them into a single string that reads out with all the characters joined together so that is how the join method works now let's take a look at the slice method

---
*Источник: https://ekstraktznaniy.ru/video/12725*