1//Creating a list
2var fruits = ["Apple", "Banana", "Cherry"];
3
4//Creating an empty list
5var books = [];
6
7//Getting element of list
8//Lists are ordered using indexes
9//The first element in an list has an index of 0,
10//the second an index of 1,
11//the third an index of 2,
12//and so on.
13console.log(fruits[0]) //This prints the first element
14//in the list fruits, which in this case is "Apple"
15
16//Adding to lists
17fruits.push("Orange") //This will add orange to the end of the list "fruits"
18fruits[1]="Blueberry" //The second element will be changed to Blueberry
19
20//Removing from lists
21fruits.splice(0, 1) //This will remove the first element,
22//in this case Apple, from the list
23fruits.splice(0, 2) //Will remove first and second element
24//Splice goes from index of first argument to index of second argument (excluding second argument)
1//create an array like so:
2var colors = ["red","blue","green"];
3
4//you can loop through an array like this:
5for (var i = 0; i < colors.length; i++) {
6 console.log(colors[i]);
7}
1var colors = ["red","blue","green"];
2
3//you can loop through an array like this:
4for (var i = 0; i < colors.length; i++) {
5 document.write(colors[i]);//printing elements of the array
6 }
7 colors.push("pink");//adds element to array;
8 colors.pop(); // removes last element of the array
9 colors.shift("purple","aqua");//adds one or more items at the front of the array
10 color.reverse();//reverses the array
11 var g=color.indexOf("green");//returns index of value
12 var c= color.includes("red");//to search for an element
13 color.slice();//splits the array
14 color.sort() ; //sorts the array