-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path22-arrays.html
61 lines (45 loc) · 1.79 KB
/
22-arrays.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
</head>
<body>
<script type="text/javascript">
// Working with Arrays
/*
Syntax to follow - var yourArrayName = [];
Anything within the `[]` will become the array
Strings go in quotes, other values are simply entered
Remember to comma delineate each value in the array
*/
var names = ["Fred", "Amy", "Bert", "Mary-Ann"];
var ages = [66, 28, 7, 3]
document.write(names[1] + ' ' + ages[1]);
// Long hand way to write out an array
/*
You don't want to do this if you are creating a simple list,
but this helps to illustrate how an array can be built and have options
to add and remove properties.
This has a downside that you have locked in the number of objects you can
add to the array, and that could be bad.
*/
var longList = new Array(3);
longList[0] = "Star Trek";
longList[1] = "Godzilla";
longList[2] = "Office Space";
document.write(longList[2]);
// Empty array
/*
A better way to do this is to create an empty array and then as you work through the
problem you are trying to solve, add the objects to the array as needed.
*/
var emptyList = [];
emptyList[0] = "Old records";
emptyList[1] = "Old shoes";
emptyList[2] = "Old people";
document.write(emptyList[1]);
</script>
</body>
</html>