JS Arrays Flashcards
JavaScript arrays are used to store multiple values in a single variable. An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
var car1 = "Saab"; var car2 = "Volvo"; var car3 = "BMW";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? The solution is an array! An array can hold many values under a single name, and you can access the values by referring to an index number.
Using an array literal is the easiest way to create a JavaScript Array.
Syntax:
var array_name = [item1, item2, …];
Using the example of cars above, create an array with the name cars containing the three values above.
var cars = ["Saab", "Volvo", "BMW"]; document.getElementById("demo").innerHTML = cars;
Spaces and line breaks are not important. A declaration can span multiple lines:
var cars = [ "Saab", "Volvo", "BMW" ];
Putting a comma after the last element (like “BMW”,) is inconsistent across browsers.
IE 8 and earlier will fail.
1)Access the Elements of an Array
You access an array element by referring to the index number.
This statement accesses the value of the first element in cars:
Note: Array indexes start with 0.
[0] is the first element. [1] is the second element.
Write code that will display the first value in the array cars…
2)Changing an Array Element
Writing the array name with the desired index number allows for the change of that index number’s value by assigning a new value (=) after the original declaration of the array. This statement changes the value of the first element in cars to:
“Opel”
3)Access the Full Array
With JavaScript, the full array can be accessed by referring to just the array name:
1)Access the Elements of an Array var cars = ["Saab", "Volvo", "BMW"]; document.getElementById("demo").innerHTML = cars[0];
2)Changing an Array Element var cars = ["Saab", "Volvo", "BMW"]; cars[0] = "Opel";
3) Access the Full Array
document. getElementById(“demo”).innerHTML = cars;
1)Arrays are Objects
Arrays are a special type of objects. The typeof operator in JavaScript returns “object” for arrays.
But, JavaScript arrays are best described as arrays.
Arrays use numbers to access its “elements”. In this example, person[0] returns John because the Array name is “person” and the array element in the 0 index position is “John”:
2)Objects
Objects use names to access its “members”. In this example, person.firstName returns “John” because the Object property firstName has the value of “John”.
Object differ from Arrays with with { } and PropertyName:PropertyValue pairs.
3)Array Elements Can Be Objects
JavaScript variables can be objects. Arrays are special kinds of objects.
Because of this, you can have variables of different types in the same Array.
You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array:
1)Arrays are Objects var person = ["John", "Doe", 46]; document.getElementById("demo").innerHTML = person[0];
2)Objects var person = {firstName:"John", lastName:"Doe", age:46}; document.getElementById("demo").innerHTML = person["firstName"];
3)Array Elements Can Be Objects You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array: myArray[0] = Date.now; myArray[1] = myFunction; myArray[2] = myCars;
Array Properties and Methods
The real strength of JavaScript arrays are the built-in array properties and methods:
// The length property returns the number of elements.
Properties are written with the name of the Array, Object or var followed by the property name in dot.notation form.
// The sort() method sorts arrays.
Methods are functions in Objects and Arrays and are written with dot.notation as well accompanied by a () at the end.
The length Property
The length property of an array returns the length of an array (the number of array elements).
The length property is always one more than the highest array index because the index starts with [0] and the length property starts with [1]. Provide the number 4 for the Array fruits.
var x = cars.length; // The length property returns the number of elements var y = cars.sort(); // The sort() method sorts arrays
var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits.length;
The length property returns the length of an array.
4
Accessing the First Array Element
JavaScript array elements are accessed using numeric indexes (starting from 0).
Create a var named first with the value of the the first indexed element in the Array fruits.
Accessing the Last Array Element
The last Array element can be accessed by creating a new var name with the value of the array name followed by square brackets with the dot.notation of array name and property length minus 1.
Accessing the First Array Element var fruits = ["Banana", "Orange", "Apple", "Mango"]; var first = fruits[0]; document.getElementById("demo").innerHTML = first;
JavaScript array elements are accesses using numeric indexes (starting from 0).
Banana
var fruits = ["Banana", "Orange", "Apple", "Mango"]; var last = fruits[fruits.length-1]; document.getElementById("demo").innerHTML = last;
Looping Array Elements
The safest way to loop through an array, is using a for loop:
The best way to loop through an array is using a standard for loop:
Banana
Orange
Apple
Mango
var fruits, text, fLen, i;
fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
fLen = fruits.length;
text = "<ul>"; for (i = 0; i < fLen; i++) { text += "<li>" + fruits[i] + "</li>"; } text += "</ul>";
document.getElementById(“demo”).innerHTML = text;