GeezerVison is showing github.com — Added Arrays chapter · git-jhy/javascript@1263801
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: SUMMARY.md +3Lines changed: 3 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | * [Else](conditional/else.md) | 19 | 19 | * [Comparators](conditional/comparators.md) | 20 | 20 | * [Concatenate](conditional/concatenate.md) | | 21 | +* [Arrays](arrays/README.md) | | 22 | + * [Indicies](indicies.md) | | 23 | + * [Length](length.md) | 21 | 24 | * [Loops](loops/README.md) | 22 | 25 | * [For](loops/for.md) | 23 | 26 | * [While](loops/while.md) | |
Collapse file Copy file name to clipboard +11Lines changed: 11 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Arrays | | 2 | + | | 3 | +Arrays are a fundamental part of programming. An array is a list of data. We can store a lot of data in one variable, which makes our code more readable and easier to understand. It also makes it much easier to perform functions on related data. | | 4 | + | | 5 | +The data in arrays are called **elements**. | | 6 | + | | 7 | +Here is a simple array: | | 8 | +```javascript | | 9 | +// 1, 1, 2, 3, 5, and 8 are the elements in this array | | 10 | +var numbers = [1, 1, 2, 3, 5, 8]; | | 11 | +``` |
Collapse file Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Indicies | | 2 | + | | 3 | +So you have your array of data elements, but what if you want to access a specific element? That is where indicies come in. An **index** refers to a spot in the array. Indicies logically progress one by one, but it should be noted that the first index in an array is 0, as it is in most languages. Brackets [] are used to signify you are referring to an index of an array. | | 4 | + | | 5 | +```javascript | | 6 | +// This is an array of strings | | 7 | +var fruits = ["apple", "banana", "pineapple", "strawberry"]; | | 8 | + | | 9 | +// We set the variable banana to the value of the second element of | | 10 | +// the fruits array. Remember that indicies start at 0, so 1 is the | | 11 | +// second element. Result: banana = "banana" | | 12 | +var banana = fruits[1]; | | 13 | +``` | | 14 | +--- | | 15 | + | | 16 | +Define the variables using the indices of the array | | 17 | + | | 18 | +```js | | 19 | +var cars = ["Mazda", "Honda", "Chevy", "Ford"] | | 20 | +var honda = | | 21 | +var ford = | | 22 | +var chevy = | | 23 | +var mazda = | | 24 | +``` | | 25 | + | | 26 | +```js | | 27 | +var cars = ["Mazda", "Honda", "Chevy", "Ford"] | | 28 | +var honda = cars[1]; | | 29 | +var ford = cars[3]; | | 30 | +var chevy = cars[2]; | | 31 | +var mazda = cars[0]; | | 32 | +``` | | 33 | + | | 34 | +```js | | 35 | +assert(honda === "Honda"); | | 36 | +assert(ford === "Ford"); | | 37 | +assert(chevy === "Chevy"); | | 38 | +assert(mazda === "Mazda"); | | 39 | +``` | | 40 | + | | 41 | +--- |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Length | | 2 | + | | 3 | +Arrays have a property called length, and it's pretty much exactly as it sounds, it's the length of the array. | | 4 | + | | 5 | +```javascript | | 6 | +var array = [1 , 2, 3]; | | 7 | + | | 8 | +// Result: l = 3 | | 9 | +var l = array.length; | | 10 | +``` | | 11 | + | | 12 | +--- | | 13 | + | | 14 | +Define the variable a to be the number value of the length of the array | | 15 | + | | 16 | +```js | | 17 | +var array = [1, 1, 2, 3, 5, 8]; | | 18 | +var l = array.length; | | 19 | +var a = | | 20 | +``` | | 21 | + | | 22 | +```js | | 23 | +var array = [1, 1, 2, 3, 5, 8]; | | 24 | +var l = array.length; | | 25 | +var a = 6; | | 26 | +``` | | 27 | + | | 28 | +```js | | 29 | +assert (a === 6) | | 30 | +``` | | 31 | +--- |
|