Chapter 3Arrays

We saw in Chapter 2 how variables store different types of data. But in the case of something like a to-do list, where you have more than one item to deal with, you're going to need a way to store a collection of data. This is where arrays come in.

Creating an Array

You can create an array in a couple of ways:

var myArray = new Array();

Or:

var myArray = [];

The [] notation is called an array literal, and it represents an empty array. It’s less verbose and safer to use than the new Array() syntax, because the Array constructor can be overwritten and potentially replaced with malicious code; for example, a function that masquerades as an array but sends any data you place in it to a third-party server on the Internet. Using the ...

Get Jump Start 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.