DEV Community

Cover image for Working with JavaScript Arrays Pt. 3
Ambush3
Ambush3

Posted on

Working with JavaScript Arrays Pt. 3

Often in JavaScript you'll come across big long strings, that you'll more than likely want to separate certain items out into a more useful form, so then you can display them differently. Like a table for instance.

To do such an action, we can use the method, split().
Split takes a single parameter, which is the character you want to separate the string at, puts these substrings into an array, then returns the array.
Let's play around with it.

*1. First we create a string. *

image.png

*2. Next, we'll split it at every comma. *

image.png

3. Lastly, we can find the length of the new array, and receive some items from it.

image.png

*4. You can do the opposite of .split(), by using the .join() method. *

image.png

5. One of the other ways of converting an array to a string, is using the ***.toString()* method.

The .toString() method is more simple than the .join() method. It doesn't take a parameter. But, it is more limiting to use.
With the .join() method, you can specify different separators, whereas with .toString() method, it always uses a comma.

image.png

Top comments (0)