Chapter 11

Creating and Changing Arrays

Who doesn’t like lists? Lists are everywhere and we use them all the time in our day-to-day lives. Whether you’re keeping track of your favorite songs, writing down everything you have to get done today, or viewing the top ten cutest animal pictures on the Internet, lists help us organize and make sense of the world.

In computer programming, we use something called an array to store lists of data. In this chapter, we show you how to create arrays, how to change the values in arrays, and how to work with arrays to do useful things inside your programs.


What Are Arrays?

Arrays are a special kind of variable that can hold multiple values using the same name. You can picture an array as being similar to a dresser with multiple drawers. Each drawer can hold something different, but the whole thing is still your dresser.

If you want to tell someone which drawer to look in for socks, you can say, “The socks are in the top drawer.” If they’re in the next drawer down, you’d say, “They’re in the second drawer from the top.” If you were to make a list showing what’s in each drawer of your dresser, you would do something like this:

  • Top drawer: Socks
  • Second drawer: Shirts
  • Third drawer: Pants

If you understand how your dresser organizes your clothes, you have a good start on understanding how JavaScript arrays organize data.

If we wanted to create ...

Get JavaScript For Kids For Dummies now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.