# Arrays

An Array is a list.

You can add or remove things from a list.

You can sort the contents of the list.

You can refer to items on the list by their numeric position.

The concept of an Array is an easy one. Arrays are also onoe of the most useful data structures that there is in JavaScript. We will be using Arrays from now until the end of the program. Arrays are your friend. Practice working with Arrays and you will become very effective at programming in JavaScript.

There are two ways to create an Array, just like with Objects. In the example below, the first one is the Array literal syntax and the second is the Array constructor.

let names = ["Sam", "Dean", "Castiel", "Crowley"];
//variable names are case-sensitive
let Names = new Array("Sam", "Dean", "Castiel", "Crowley");

names.push("Rowena"); //add the name Rowena to the names  array
names.push("Charlie");

Names.push("Chuck"); //add the name Chuck to the Names array
Names.push("Amara");
Names.push("Mary"); //add the name Mary at the end of Names
Names.pop(); //Remove the last value in Names

console.log(names[0]); //Sam
console.log(names.length); //6
console.log(names[5]); //Charlie
//Note how (names.length - 1) is the index number of the last item in the Array
console.log(Names[1]); //Dean
console.log(Names[4]); //Amara
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

When reading the console log statements you would say "names sub zero", "names sub one", and so on. Watch this video to learn more about Array fundamentals.

If you are looking for more information about Arrays, there is a playlist about Arrays that will give you everything you need to know about them. Full Array video playlist There are 35 videos in the playlist. You don't need to watch them all this week. Some of the topics we will be discussing next semester.

MDN Array reference

Back to Week 3 main page

Last Updated: 6/13/2020, 11:30:19 PM