You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Arrays are zero-indexed, ordered lists of values. They are a handy way to store a set of related items of the same type (such as strings), though in reality, an array can include multiple types of items, including other arrays.
9
10
10
11
To create an array, either use the object constructor or the literal declaration, by assigning the variable a list of values after the declaration.
11
12
12
13
```
13
14
// A simple array with constructor.
14
15
var myArray1 = new Array( "hello", "world" );
16
+
15
17
// Literal declaration, the preferred way.
16
18
var myArray2 = [ "hello", "world" ];
17
19
```
@@ -22,22 +24,24 @@ If the values are unknown, it is also possible to declare an empty array, and ad
22
24
23
25
```
24
26
// Creating empty arrays and adding values
27
+
25
28
var myArray = [];
26
29
27
-
// adds "hello" on index 0
30
+
// Adds "hello" on index 0
28
31
myArray.push( "hello" );
29
32
30
-
// adds "world" on index 1
33
+
// Adds "world" on index 1
31
34
myArray.push( "world" );
32
35
33
-
// adds "!" on index 2
36
+
// Adds "!" on index 2
34
37
myArray[ 2 ] = "!";
35
38
```
36
39
37
40
`.push()` is a function that adds an element on the end of the array and expands the array respectively. You also can directly add items by index. Missing indices will be filled with `undefined`.
38
41
39
42
```
40
43
// Leaving indices
44
+
41
45
var myArray = [];
42
46
43
47
myArray[ 0 ] = "hello";
@@ -51,9 +55,10 @@ If the size of the array is unknown, `.push()` is far more safe. You can both ac
51
55
52
56
```
53
57
// Accessing array items by index
58
+
54
59
var myArray = [ "hello", "world", "!" ];
55
60
56
-
console.log( myArray[2] ); // "!"
61
+
console.log( myArray[ 2 ] ); // "!"
57
62
```
58
63
59
64
## Array Methods and Properties
@@ -64,6 +69,7 @@ The `.length` property is used to determine the amount of items in an array.
64
69
65
70
```
66
71
// Length of an array
72
+
67
73
var myArray = [ "hello", "world", "!" ];
68
74
69
75
console.log( myArray.length ); // 3
@@ -73,21 +79,27 @@ You will need the `.length` property for looping through an array:
73
79
74
80
```
75
81
// For loops and arrays - a classic
82
+
76
83
var myArray = [ "hello", "world", "!" ];
77
84
78
85
for ( var i = 0; i < myArray.length; i = i + 1 ) {
`.join()` creates a string representation of an array by joining all of its elements using a separator string. If no separator is supplied (e.g. `.join()` is called without arguments) the array will be joined using a comma:
118
+
`.join()` creates a string representation of an array by joining all of its elements using a separator string. If no separator is supplied (e.g. `.join()` is called without arguments) the array will be joined using a comma.
110
119
111
120
```
112
121
// Joining elements
122
+
113
123
var myArray = [ "hello", "world", "!" ];
114
124
115
-
// The default separator is a comma
125
+
// The default separator is a comma.
116
126
console.log( myArray.join() ); // "hello,world,!"
117
127
118
128
// Any string can be used as separator...
119
-
console.log( myArray.join(" ") ); // "hello world !";
`.pop()` removes the last element of an array. It is the opposite method of `.push()`:
130
140
131
141
```
132
-
// pushing and popping
142
+
// Pushing and popping
143
+
133
144
var myArray = [];
134
145
135
146
myArray.push( 0 ); // [ 0 ]
@@ -143,19 +154,17 @@ myArray.pop(); // [ 0 , 2 ]
143
154
As the name suggests, the elements of the array are in reverse order after calling this method:
144
155
145
156
```
146
-
// reverse
147
157
var myArray = [ "world" , "hello" ];
148
-
149
-
// [ "hello", "world" ]
150
-
myArray.reverse();
158
+
myArray.reverse(); // [ "hello", "world" ]
151
159
```
152
160
153
161
### .shift()
154
162
155
-
Removes the first element of an array. With `.push()` and `.shift()`, you can recreate the method of a [queue](http://en.wikipedia.org/wiki/Queue_(data_structure)):
163
+
Removes the first element of an array. With `.push()` and `.shift()`, you can recreate the method of a [queue](http://en.wikipedia.org/wiki/Queue_(abstract_data_type%29):
156
164
157
165
```
158
-
// queue with shift() and push()
166
+
// Queue with shift() and push()
167
+
159
168
var myArray = [];
160
169
161
170
myArray.push( 0 ); // [ 0 ]
@@ -169,7 +178,8 @@ myArray.shift(); // [ 2 , 7 ]
169
178
Extracts a part of the array and returns that part in a new array. This method takes one parameter, which is the starting index:
The return value of descending (for this example) is important. If the return value is less than zero, the index of a is before b, and if it is greater than zero it's vice-versa. If the return value is zero, the elements index is equal.
235
+
The return value of descending (for this example) is important. If the return value is less than zero, the index of `a` is before `b`, and if it is greater than zero it's vice-versa. If the return value is zero, the elements' index is equal.
226
236
227
237
### .unshift()
228
238
229
239
Inserts an element at the first position of the array:
230
240
231
241
```
232
-
// unshift
233
242
var myArray = [];
234
243
235
244
myArray.unshift( 0 ); // [ 0 ]
@@ -243,14 +252,15 @@ In modern browsers it is possible to traverse through arrays with a `.forEach()`
243
252
244
253
The function takes up to three arguments:
245
254
246
-
**Element*- The element itself.
247
-
**Index*- The index of this element in the array.
248
-
**Array*- The array itself.
255
+
**Element*– The element itself.
256
+
**Index*– The index of this element in the array.
257
+
**Array*– The array itself.
249
258
250
-
All of these are optional, but you will need at least the "Element" parameter in most cases.
259
+
All of these are optional, but you will need at least the *Element* parameter in most cases.
0 commit comments