This script creates a new array called temp
and then adds elements to it using the square bracket notation:
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
var temp = new Array();
temp[0] = "Prolog";
temp[1] = "Lisp";
temp[2] = "C++";
temp[50] = "Perl";
document.write(temp);
document.write("<br><br>");
document.write("If we search for something that's not there:");
document.write("<br><b>" + temp[25] + "</b>");
</script>
</body>
</html>
temp[0] = "Prolog"
sets the value of the first element to "Prolog".
temp[1] = "Lisp"
sets the value of the second element to "Lisp".
temp[2] = "C++"
sets the value of the third element to "C++".
temp[50] = "Perl"
sets the value of the 51st element to "Perl".
The script then uses document.write
to display the entire array. When the array is displayed, you'll notice that there are many empty elements (from index 3 to 49) because they were not explicitly set.
The script then tries to access an element at index 25 (temp[25]
) which was not explicitly set, so it will display undefined
.
No comments:
Post a Comment