# Arrays in JavaScript by [kirupa](https://www.kirupa.com/me/index.htm) | 17 December 2013 Let's imagine you are jotting down a list on a piece of paper. Let's call the piece of paper **groceries**. Now, in the paper, you write a numbered list starting with zero with all the items that belong there: ![](../flash/images/restaurant.png) What you have right now is a real-world example of an array! The piece of paper called "groceries" would be your array. The items that you need to purchase are known as the array values. In this tutorial, you will learn all about what I like to go grocery shopping for. You may indirectly get an introduction to the most common things you want to do with an array. ## Declaring an Array While you do not have to declare variables before using them in JavaScript, it is a good practice to follow if you like readable code, ice cream, and puppies. ### Array Literal Notation The popular way to declare arrays these days is to use what is known as **array literal notation**. As you will see shortly throughout this tutorial, it is a really simple syntax made up of just brackets for working with arrays and their values. Let's start at the very top and create an empty array: ``` var groceries = []; ``` You have your variable name on the left, and you have a pair of brackets on the right that initializes this variable as an array. By only having the brackets with nothing inside or outside of it, you create an empty array. You will commonly want to create an array with some items inside it from the very beginning. To create these non-empty arrays, place the items you want inside the brackets and separate them by commas: ``` var groceries = ["Milk", "Eggs", "Frosted Flakes", "Salami", "Juice"]; ``` Notice that my groceries array now contains **Milk**, **Eggs**, **Frosted Flakes**, **Salami**, and **Juice**. I just have to reiterate how important the commas are. Without the commas, you'll just have one giant item instead. ### Using the Array Constructor There is another way to declare an array, and that is by creating an `Array` object using the `new` keyword: ``` var groceries = new Array(); ``` The above line creates an empty array. If you wish to populate the array with some initial values, you can pass the values in as arguments to your constructor: ``` var groceries = new Array("Milk", "Eggs", "Frosted Flakes", "Salami", "Juice"); ``` From a JavaScript runtime point of view, there is no change in the end result of using this approach or the array literal notation approach. There are some changes under the covers in how the object is represented compared to the literal approach, but we'll look at that later. Don't worry about it for now. So, which approach should you use? There have been debates online on which approach is better with good arguments presented on both sides. I am a fan of the array literal notation approach, but if you were to use the `new` keyword with the `Array` constructor, you'll still be invited to my birthday party! #### Note With the `Array` constructor, note that the following code does not set the first value of your array to a '5': ``` var groceries = new Array(5); ``` What it means is that your array will be created with a length of 5. The values inside this array will be set to `undefined`. All right, now that you've learned how to declare an array. Let's look at how you can actually use it to store and work with data. ## Accessing Array Values One of the nice things about arrays is that you not only have easy access to the array, but you also have easy access to the array values...similar to highlighting an item in your grocery list: ![](../flash/images/highlighting_menu.png) The only thing you need to know is what the procedure is for accessing an individual item. Inside an array, each item is assigned a number starting with zero. In the above example, **Milk** would be given the value 0, **Eggs** the value 1, **Frosted Flakes** the value 2, and so on. The formal term for these numbers is called the index value. Let's say that our groceries array is declared as follows: ``` var groceries = ["Milk", "Eggs", "Frosted Flakes", "Salami", "Juice"]; ``` If I wanted to access an item from the array, all I need to do is pass in the index value of the item I am interested in: ``` groceries[1] ``` The index value is passed in to your array using square brackets. In this example, you are referring to the Eggs value because the index position 1 refers to it. If you passed in a 2, you would return **Frosted Flakes**. You can keep passing in a index value until you have no more values left. The range of numbers you can use as your index values is one less than your array's length. The reason is that, as shown in the diagram earlier, your index values start with a value o 0. If your array only has 5 items, trying to display `grocery[6]` or `grocery[5]` will result in a message of **undefined**. Let's go one step further. In most real world scenarios, you will want to programmatically go through your array as opposed to accessing each item individually. You can take what I explained in the previous paragraph and use a `for` loop to accomplish this: ``` for (var i=0; i