Chapter 3

Arrays

In Chapter 2, we saw that strings can be thought of as sequences of characters in a particular order. In this chapter, we’ll learn about the array data type, which is the general JavaScript container for arbitrary elements in a particular order. We’ll start by explicitly connecting strings and arrays via the String split method (Section 3.1), and then learn about various array methods throughout the rest of the chapter.

3.1 Splitting

So far we’ve spent a lot of time understanding strings, and there’s a natural way to get from strings to arrays via the split method:

> "ant bat cat".split("  ");     // Split a string into a three-element array.
[ 'ant', 'bat', 'cat' ]

We see from this result that split ...

Get Learn Enough JavaScript to Be Dangerous: Write Programs, Publish Packages, and Develop Interactive Websites with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.