Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
Added Explanation for Array Push (#1328)
Browse files Browse the repository at this point in the history
Basic information on the push method with example. How to use it and when to use it.
  • Loading branch information
loki-one authored and ChaituVR committed Oct 22, 2017
1 parent 6c14b21 commit 7517c10
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,27 @@ This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/
#### More Information:
<!-- Please add any articles you think might be helpful to read before writing the article -->

### Basic Information:
This is a method on Array's to add elements on the end of the an array.

`var myStarkFamily = ['John', 'Sansa', 'Brian'];`

### Explanation:
Now I have an array which contains members of Stark family from Game of thrones. But you can see that one of the member is missing.
That is **Arya** and I want to add it.
At this moment I know the length of an Array which is 3.
So I can add it as follows:

`myStarkFamily[3] = 'Arya';`

Yes you can add it this way. But what if you donot know the length of and Array and want to add an element at the end.
Then you do it as follows:

`myStackFamily.push('Arya');`
This returns you the new length of the array i.e. 4 and **Arya** is added to your Stark Family Array.
Hence when you `console.log(myStarkFamily);` Your output is: `['John', 'Sansa', 'Brian', 'Arya'];`

### In which case to use it:
From above explanation it is clear that, you can use it in following case:
1) When you want to add an element at the end of an Array.
2) And you do not know how many elements are there in an array. So you can use push() on the array an pass the value you want to add.

0 comments on commit 7517c10

Please sign in to comment.