Section Introduction: Introduction to JavaScript Arrays
In this section of the course, we're going to talk about the array data structure in JavaScript.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a free Dev Camp account

Now, if you've never heard of an array or you've never used them before at a high level: what an array is, is it is a collection. So it gives you the ability to store a collection of data. So whereas with a standard variable, so say when you store a string like a name that is just a single data object it's a single string.

Well, what happens if you need to store a large number of names. Well, that is where arrays come in. As you go along your coding journey, you're going to discover that arrays are used throughout the entire JavaScript world.

Any time that JavaScript is going to store a list of items, it's typically going to be in an array or a data structure very similar to it. For example, if you're building out a directory site kind of like a Yellow Pages or a Yelp kind of site, and you contact an API. That API is going to send you a list or a collection of items back.

The way that JavaScript is going to interpret that is it's going to place that inside of an array. So if you're calling a database, if you're calling an API, if you're getting any kind of collection, JavaScript is just about always going to put it into an array. That's why it's so important to be able to understand what arrays are and also how to work with them.

The real-world analogy that I like to give to really help explain and solidify why arrays are so important is: imagine that you're going to the grocery store. If you go to the grocery store for just a few items, then you can just pick those up put them in your hands, and then carry them to the cash register.

large

If you go and you have 100 items to get or even 12 items to get, any number that you can't really carry in your hands, you need something to put those into. You need a shopping cart typically.

large

Well, inside of JavaScript that's what you use arrays for. You can really only store so many items inside of a variable, such as just you can put one number in a variable, or you can put one string in a variable. You can only really have so many variables before it starts to get a little messy.

If you have the same data type, so if you have a bunch of usernames or restaurant listings or blog posts, then typically you're going to store those in an array. In the same way that when you go to the grocery store, it starts to get very unwieldy if you started carrying all of your groceries just in your hands.

Same thing holds true for JavaScript. You want to be able to have a data structure that you can wrap all of those objects in, then you can store that single array inside of a variable, and then you can work with it. With all that being said, let's dive into the code and start building out some arrays in JavaScript.