Book
Submodule 7.1: Arrays
Submodule 7.1: Arrays
Completion requirements
View
- Definitions
- Accessing an array
- Array methods
How can I access an array?
Access the Full Array
With JavaScript, we can access an array by referencing its name:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Accessing an array </title>
</head>
<body>
<h3>Accessing an array </h3>
<div id="thearray"></div>
<script>
var myArray = [];
myArray[0] = "cat";
myArray[1] = 1;
myArray[2] = true;
myArray[3] = ["a", "b"];
alert (myArray);
document.getElementById("thearray").innerHTML = myArray;
console.log (myArray);
</script>
</body>
</html>
Access the elements of an array
Also, we can access the array elements by referring to the index number:
console.log (myArray[2]) /*true*/
Exercise
- Open your editor, create a new file and save it as
exersice07.1.02.html
in the folder "yourNameWEB2JS". - Copy the above code and paste it into the new file.
- Save the file and preview it in your editor or your browser. Does everything appear right? Inspect you Console to see the results.