|
2 | 2 | chapter : js101
|
3 | 3 | section: 6
|
4 | 4 | title: Arrays
|
5 |
| -attribution: jQuery Fundamentals, Stefan Baumgartner (@ddprrt) |
| 5 | +attribution: |
| 6 | +- jQuery Fundamentals |
| 7 | +- Stefan Baumgartner |
| 8 | +github: ddprrt |
6 | 9 | ---
|
7 |
| -Arrays are zero-indexed lists of values. They are a handy way to store a set of |
| 10 | +Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of |
8 | 11 | related items of the same type (such as strings), though in reality, an array
|
9 |
| -can include multiple types of items, including other arrays. Arrays in Javascript |
10 |
| -work bascially like in most other programming or scripting languages, although there |
11 |
| -are some subtle differences. |
| 12 | +can include multiple types of items, including other arrays. |
12 | 13 |
|
13 | 14 | To create an array you can either use the object constructor or the literal declaration,
|
14 |
| -by assign your variable a list of values right after the declaration. |
| 15 | +by assigning your variable a list of values right after the declaration. |
15 | 16 |
|
16 | 17 | <javascript caption="A simple array">
|
17 | 18 | var myArray1 = new Array( 'hello', 'world' ); // with constructor
|
18 |
| -var myArray2 = [ 'hello', 'world' ]; // literal declaration, the prefered way |
| 19 | +var myArray2 = [ 'hello', 'world' ]; // literal declaration, the preferred way |
19 | 20 | </javascript>
|
20 | 21 |
|
| 22 | +The literal declaration is preferred, see the |
| 23 | +[Google Coding Guidelines](http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Array_and_Object_literals) |
| 24 | +for more information. |
| 25 | + |
21 | 26 | If you don't know your values yet, it is also possible to declare an empty Array, and
|
22 | 27 | add elements either through functions or through accessing by index:
|
23 | 28 |
|
24 | 29 | <javascript caption="Creating empty arrays and adding values">
|
25 |
| -var myArray = []; // var myArray = new Array(); would also work |
| 30 | +var myArray = []; |
26 | 31 |
|
27 | 32 | myArray.push('hello'); // adds 'hello' on index 0
|
28 | 33 | myArray.push('world'); // adds 'world' on index 1
|
|
0 commit comments