-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_array_objects.js
More file actions
81 lines (61 loc) · 2.47 KB
/
05_array_objects.js
File metadata and controls
81 lines (61 loc) · 2.47 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Project: cc_javascript_intro
* File: 05_array_objects.js
* Author: nico
* Created: 2016-02-20
*
* Description:
* JavaScript file to illustrate the minimal basics of the JavaScript scripting language.
* This file illustrates the concepts of arrays and objects.
*/
function js05_arrayObjects() {
arrayDemo();
objectDemo();
}
function arrayDemo() {
// Arrays are collections of multiple values ("array elements") within one variable.
// array declaration and initialization
var animals = ['cat', 'dog', 'moose', 'bird', 'fox'];
// examining the single elements of an array collection
// Note: The first index is 0, not 1!
console.log("Index 0 = ", animals[0]);
console.log("Index 1 = ", animals[1]);
console.log("Index 2 = ", animals[2]);
console.log("Index 3 = ", animals[3]);
console.log("Index 4 = ", animals[4]);
// assigning a new value to an array element
animals[4] = 'wolf';
console.log("Index 4 = ", animals[4]);
// arrayname.length returns the amount of elements in the array
console.log("Amount of animals = ", animals.length);
// arrayname.push(element) adds a new element at the end of the current array
animals.push("Octopus");
console.log("Amount of animals = ", animals.length);
// basic rule: arrayname.length - 1 returns the last valid array index
console.log("Last index = ", animals.length - 1);
console.log("Last animal = ", animals[animals.length - 1]);
}
function objectDemo() {
// Objects are collections of multiple key-value pairs within one variable.
// object declaration and initialization
var zelda = { name: "The Legend of Zelda", year: 1986, developer: "Nintendo"};
var mario = { name: "Super Mario World", year: 1990, developer: "Nintendo"};
// individual values of an object can be access using objectname.key ("dot" notation)
console.log(zelda.name);
console.log(zelda.year);
console.log(zelda.developer);
console.log(mario.name);
console.log(mario.year);
console.log(mario.developer);
// assigning new values to some keys of an existing object
mario.name = "Super Mario 64";
mario.year = 1996;
console.log(mario.name);
console.log(mario.year);
// additing a new key-value pair to an existing object
mario.brother = "Luigi";
console.log(mario.brother);
// console.log(objectname) prints all information about an object
console.log(zelda);
console.log(mario);
}