@@ -12,10 +12,11 @@ can include multiple types of items, including other arrays.
1212To create an array you can either use the object constructor or the literal declaration,
1313by assigning your variable a list of values right after the declaration.
1414
15- <javascript caption =" A simple array " >
15+ ``` js
16+ // A simple array
1617var myArray1 = new Array ( ' hello' , ' world' ); // with constructor
1718var myArray2 = [ ' hello' , ' world' ]; // literal declaration, the preferred way
18- </ javascript >
19+ ```
1920
2021The literal declaration is preferred, see the
2122[ Google Coding Guidelines] ( http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml#Array_and_Object_literals )
@@ -24,175 +25,192 @@ for more information.
2425If you don't know your values yet, it is also possible to declare an empty Array, and
2526add elements either through functions or through accessing by index:
2627
27- <javascript caption =" Creating empty arrays and adding values " >
28+
29+ ``` js
30+ // Creating empty arrays and adding values
2831var myArray = [];
2932
3033myArray .push (' hello' ); // adds 'hello' on index 0
3134myArray .push (' world' ); // adds 'world' on index 1
3235myArray[2 ] = ' !' ; // adds '!' on index 2
33- </ javascript >
36+ ```
3437
3538'push' is a function which adds an element on the end of the array and expands the array
3639respectively. You also can directly add items by index. Missing indices will be filled
3740with 'undefined';
3841
39- <javascript caption =" Leaving indices " >
42+ ``` js
43+ // Leaving indices
4044var myArray = [];
4145
4246myArray[0 ] = ' hello' ;
4347myArray[1 ] = ' world' ;
4448myArray[3 ] = ' !' ;
4549
4650console .log (myArray); // logs ['hello', 'world', undefined, '!'];
47- </ javascript >
51+ ```
4852
4953So 'push' is far more safe, especially if you don't know the size of your
5054array yet. With the index you not only assign values to array items, but also
5155access those.
5256
53- <javascript caption =" Accessing array items by index " >
57+ ``` js
58+ // Accessing array items by index
5459var myArray = [ ' hello' , ' world' , ' !' ];
5560console .log (myArray[2 ]); // logs '!'
56- </ javascript >
61+ ```
5762
5863### Array methods and properties
5964
6065## length
6166
6267The 'length' property is used to know the amount of items in your array.
6368
64- <javascript caption =" Length of an array " >
69+ ``` js
70+ // Length of an array
6571var myArray = [ ' hello' , ' world' , ' !' ];
6672console .log (myArray .length ); // logs 3
67- </ javascript >
73+ ```
6874
6975You will need the length property for looping through an array:
7076
71- <javascript caption =" For loops and arrays - a classic " >
77+ ``` js
78+ // For loops and arrays - a classic
7279var myArray = [' hello' , ' world' , ' !' ];
7380for (var i = 0 ; i < myArray .length ; i = i + 1 ) {
7481 console .log (myArray[i]);
7582}
76- </ javascript >
83+ ```
7784
7885Except when you are using for ... in loops:
7986
80- <javascript caption"For loops and arrays - alternate method">
87+ ``` js
88+ // For loops and arrays - alternate method
8189var myArray = [' hello' , ' world' , ' !' ];
8290for (var i in myArray) {
8391 console .log (myArray[i]);
8492}
85- </ javascript >
93+ ```
8694
8795## concat
8896
8997With 'concat', you can concatenate two or more arrays
9098
91- <javascript caption =" Concatenating Arrays " >
99+ ``` js
100+ // Concatenating Arrays
92101var myArray = [2 , 3 , 4 ];
93102var myOtherArray = [5 , 6 , 7 ];
94103var wholeArray = myArray .concat (myOtherArray); // [2, 3, 4, 5, 6, 7]
95- </ javascript >
104+ ```
96105
97106## join
98107
99108'join' creates a string representation of your array. It's parameter is as string
100- which works as a seperator between elements (default is a comma);
109+ which works as a separator between elements (default is a comma);
101110
102- <javascript caption =" Joining elements " >
111+ ``` js
112+ // Joining elements
103113var myArray = [' hello' , ' world' , ' !' ];
104114console .log (myArray .join (' ' )); // logs "hello world !";
105115console .log (myArray .join ()); // logs "hello,world,!"
106116console .log (myArray .join (' ' )); // logs "helloworld!"
107117console .log (myArray .join (' !!' )) // logs "hello!!world!!!!!";
108- </ javascript >
118+ ```
109119
110120## pop
111121
112122'pop' removes the last element of an array. It is the opposite method to 'push'
113123
114- <javascript caption =" pushing and popping " >
124+ ``` js
125+ // pushing and popping
115126var myArray = [];
116127myArray .push (0 ); // [ 0 ]
117128myArray .push (2 ); // [ 0 , 2 ]
118129myArray .push (7 ); // [ 0 , 2 , 7 ]
119130myArray .pop (); // [ 0 , 2 ]
120- </ javascript >
131+ ```
121132
122133## reverse
123134
124135As the name suggests, the elements of the array are in reverse order after calling
125136this method
126137
127- <javascript caption =" reverse " >
138+ ``` js
139+ // reverse
128140var myArray = [ ' world' , ' hello' ];
129141myArray .reverse (); // [ 'hello', 'world' ]
130- </ javascript >
142+ ```
131143
132144## shift
133145
134146Removes the first element of an array. With 'pop' and 'shift' you can recreate the
135147method of a [ queue] ( http://en.wikipedia.org/wiki/Queue_(data_structure) )
136148
137- <javascript caption =" queue with shift() and pop() " >
149+ ``` js
150+ // queue with shift() and pop()
138151var myArray = [];
139152myArray .push (0 ); // [ 0 ]
140153myArray .push (2 ); // [ 0 , 2 ]
141154myArray .push (7 ); // [ 0 , 2 , 7 ]
142155myArray .shift (); // [ 2 , 7 ]
143- </ javascript >
156+ ```
144157
145158## slice
146159
147160Extracts a part of the array and returns them in a new one. This method takes one
148161parameter, which is the starting index.
149162
150- <javascript caption =" slicing " >
163+ ``` js
164+ // slicing
151165var myArray = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ];
152166var newArray = myArray .slice (3 );
153167
154168console .log (myArray); // [1, 2, 3, 4, 5, 6, 7, 8]
155169console .log (newArray); // [4, 5, 6, 7, 8]
156- </ javascript >
170+ ```
157171
158172## splice
159173
160174Removes a certain amount of elements and adds new ones at the given index. It takes
161175at least 3 parameters
162176
163- <javascript caption =" splice method " >
177+ ``` js
178+ // splice method
164179myArray .splice (idx, len, values, ... );
165- </ javascript >
180+ ```
166181
167182* idx = the starting index
168183* len = the number of elements to remove
169184* values = the values which should be inserted at idx
170185
171186For example:
172187
173- <javascript caption =" splice example " >
188+ ``` js
189+ // splice example
174190var myArray = [0 , 7 , 8 , 5 ];
175191myArray .splice (1 , 2 , 1 , 2 , 3 , 4 );
176192console .log (myArray); // [0, 1, 2, 3, 4, 5]
177- </ javascript >
193+ ```
178194
179195## sort
180196
181197Sorts an array. It takes one parameter, which is a comparing function. If this function is not
182198given, the array is sorted ascending
183199
184- <javascript caption =" sorting without comparing function " >
200+ ``` js
201+ // sorting without comparing function
185202var myArray = [3 , 4 , 6 , 1 ];
186203myArray .sort (); // 1, 3, 4, 6
187- </ javascript >
204+ ```
188205
189- <javascript caption =" sorting with comparing function " >
206+ ``` js
207+ // sorting with comparing function
190208function descending (a , b ) {
191209 return b - a;
192210}
193211var myArray = [3 , 4 , 6 , 1 ];
194212myArray .sort (descending); // [6, 4, 3, 1]
195- </ javascript >
213+ ```
196214
197215The return value of descending (for this example) is important. If the return value is
198216less than zero, the index of a is before b, and if it is greater than zero it's vice-versa.
@@ -202,12 +220,13 @@ If the return value is zero, the elements index is equal.
202220
203221Inserts an element at the first position of the array
204222
205- <javascript caption =" unshift " >
223+ ``` js
224+ // unshift
206225var myArray = [];
207226myArray .unshift (0 ); // [ 0 ]
208227myArray .unshift (2 ); // [ 2 , 0 ]
209228myArray .unshift (7 ); // [ 7 , 2 , 0 ]
210- </ javascript >
229+ ```
211230
212231## forEach
213232
@@ -222,7 +241,8 @@ The function takes up to three arguments:
222241
223242All of the are optional, but you will need at least the 'element' parameter in most cases.
224243
225- <javascript caption =" native forEach " >
244+ ``` js
245+ // native forEach
226246function printElement (elem ) {
227247 console .log (elem);
228248}
@@ -239,4 +259,4 @@ myArray = [1, 2, 3, 4, 5];
239259myArray .forEach (printElement); // prints all elements to the console
240260myArray .forEach (printElementAndIndex); // prints "Index 0: 1" "Index 1: 2" "Index 2: 3" ...
241261myArray .forEach (negateElement); // myArray is now [-1, -2, -3, -4, -5]
242- </ javascript >
262+ ```
0 commit comments