GeezerVison is showing github.com — Clarifying code · TomaszMoroz/JavaScript-Lessons@093c729
File tree
Expand file treeCollapse file tree
Open diff view settings
Data_Structures/Arrays/Basics
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: Data_Structures/Arrays/Basics/main.js +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +// to declare and initialize an array, you can pass all the items in as a list |
1 | 2 | var food = ['bacon', 'corn', 'ham', 'sausage']; |
2 | 3 |
|
3 | | -// array values are retrieved by index, not name |
| 4 | +// array values are retrieved by index (starting at 0), not name |
4 | 5 | console.log(food[2]); |
5 | 6 | console.log(food.length); |
6 | 7 |
|
7 | | -// loop over an array |
| 8 | +// you can iterate over an array to access each individual element |
8 | 9 | for(var i=0; i<food.length; i++){ |
9 | 10 | console.log(i, food[i]); |
10 | 11 | } |
11 | 12 |
|
12 | | -console.log('----------'); |
13 | | - |
14 | 13 | food.forEach(function (index, item) { |
15 | 14 | console.log(item, index); |
16 | 15 | }); |
@@ -44,6 +43,6 @@ console.log(food); |
44 | 43 |
|
45 | 44 | console.log('--------------------'); |
46 | 45 |
|
47 | | -let numbers = [10, 32, 53, 99, 60]; |
| 46 | +var numbers = [10, 32, 53, 99, 60]; |
48 | 47 | console.log(numbers.toString()); // comma separated list |
49 | 48 | console.log(numbers.join('~')); // to use a different separator |