forked from GitbookIO/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds basic array methods RE: Issue GitbookIO#87
Adds methods.md file describing 4 basic array methods: Push, Pop, Shift and Unshift.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Methods | ||
|
||
Arrays can be manipulated, searched, and modified using a number of <strong>Methods</strong> that are built in to the javascript language. | ||
- A Method can be defined as a function that is stored in an object (Each javascript array is, in a deep technical sense, an object.) | ||
|
||
|
||
ADD & REMOVE <strong>LAST</strong> ELEMENTS FROM AN ARRAY | ||
|
||
- The PUSH method adds a new element to the end of any array. What element? Whatever element you specify as the argument. ::smile:: | ||
```javascript | ||
var array1 = [1, 2, 3]; | ||
array1.push(4); | ||
// RESULT: array = [1 , 2, 3, 4]; (The number 4 is PUSHED in) | ||
``` | ||
|
||
- The POP method removes the last element of an array. It does not require an argument. | ||
```javascript | ||
var array2 = [1, 2, 3, 4]; | ||
array2.pop(); | ||
// RESULT: array2 = [1 , 2, 3]; (The last item is POPPED off) | ||
``` | ||
|
||
ADD & REMOVE <strong>FIRST</strong> ELEMENTS FROM AN ARRAY | ||
|
||
- The UNSHIFT method adds new elements sequentially to the start, or front of the array. | ||
```javascript | ||
var array3 = [0, 5, 10]; | ||
array.unshift(-5); | ||
// RESULT: array3 = [-5 , 0, 5, 10]; | ||
``` | ||
|
||
- The SHIFT method removes the first element of the array and, as if replacing a vacuum in space, SHIFTS all the other elements to the left. | ||
```javascript | ||
var array4 = [5, 6, 7, 8]; | ||
array4.shift(); | ||
// RESULT: array4 = [6, 7, 8]; | ||
``` | ||
|
||
*BONUS!: You can add more than one element at once, and they will be added sequentially. | ||
```javascript | ||
var array3 = [0, 5, 10]; | ||
array3.unshift(-10, -5); | ||
// RESULT: array3 = [-10, -5 , 0, 5, 10]; | ||
``` |