Hands-on Project: Array Operation
Example 50:
(1) Open a Notepad, and write the JavaScript code to the editor.
<script>
var arr = new Array(); // creates an array
arr[0] = "Hello, ";
arr[1] = "My ";
arr[2] = "friends!"
alert(arr[0] + arr[1] + arr[2]);
</script>
|
(2) Save the above file as “myArray.html”, because we will run the program with a browser, save it as an html file.
(3) Double click the myArray.html, run the program with a web browser. We will see the output:
Output:
Hello, My friends!
Explanation:
“var arr = new Array()” creates an array, its name is “arr”.
“alert(arr[0] + arr[1] + arr[2]);” shows the value of arr[0], arr[1] and arr[2].