<commit_before>---
layout: post
title: Beginners Guide to JavaScript Array Methods - Mutator Methods
permalink: /javascript-array-mutator-methods/
teaser: javascript-arrays.jpg
excerpt: We explore nine powerful array mutator methods in JavaScript and discuss the applications of each one of them 
---

Arrays are powerful data structures that permit storing multiple values in a single variable. Arrays are actually  specialised **Objects** in JavaScript that can hold values of any type, not necessarily in key/value pairs but rather, in numerically indexed positions.

```javascript
//Creating an array
var lang = ["JavaScript", "Ruby", "Go", "Python"];
//Accessing an array item
console.log(lang[0]); //JavaScript
typeof lang;  //returns Object
```

Since arrays are actually JavaScript objects, they can have **methods** which are, in simple terms, properties of an object that refer to function values.

What actually happens when you create a new array, is that you create *an instance* of the Array constructor object which has a number of built-in methods. 

```javascript
var social = ["facebook", "twitter"];
//push() is a method of the Array constructor object
social.push("reddit"); 
console.log(social); // ["facebook", "twitter", "reddit"]
typeof social.push() //function
```

So your array inherits all of those methods automatically which is why we can use the `push()` method instantly on the `social` array in the example above without doing any further work.

Array methods are generally divided into 3 categories:

- [Mutator Methods](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Mutator_methods)
- [Accessor Methods](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Accessor_methods)
- [Iteration Methods](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Iteration_methods)

We shall consider the mutator methods in this article. They are those that modify an array in some way.  In the coming weeks, I will publish the second and third parts of this series which will consider the accessor and iteration methods.

We have nine mutator methods in JavaScript and we shall go over each one in turn. Let's begin:

## Array.pop()

The `pop()` method removes the **last** element in an array and returns that element.

Here's a simple example:

```javascript
var cities = ["London", "Lagos", "New York", "Accra"];

var lastCity = cities.pop();

console.log(cities); //["London", "Lagos", "New York"]<commit_msg>Updated link to part 2 of the article<commit_after>---
layout: post
title: Beginners Guide to JavaScript Array Methods - Mutator Methods (Part 1)
permalink: /javascript-array-mutator-methods/
teaser: javascript-arrays.jpg
excerpt: We explore nine powerful array mutator methods in JavaScript and discuss the applications of each one of them 
---

Arrays are powerful data structures that permit storing multiple values in a single variable. Arrays are actually  specialised **Objects** in JavaScript that can hold values of any type, not necessarily in key/value pairs but rather, in numerically indexed positions.

```javascript
//Creating an array
var lang = ["JavaScript", "Ruby", "Go", "Python"];
//Accessing an array item
console.log(lang[0]); //JavaScript
typeof lang;  //returns Object
```

Since arrays are actually JavaScript objects, they can have **methods** which are, in simple terms, properties of an object that refer to function values.

What actually happens when you create a new array, is that you create *an instance* of the Array constructor object which has a number of built-in methods. 

```javascript
var social = ["facebook", "twitter"];
//push() is a method of the Array constructor object
social.push("reddit"); 
console.log(social); // ["facebook", "twitter", "reddit"]
typeof social.push() //function
```

So your array inherits all of those methods automatically which is why we can use the `push()` method instantly on the `social` array in the example above without doing any further work.

Array methods are generally divided into 3 categories:

- [Mutator Methods](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Mutator_methods)
- [Accessor Methods](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Accessor_methods)
- [Iteration Methods](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype#Iteration_methods)

We shall consider the mutator methods in this article. They are those that modify an array in some way.  In the coming weeks, I will publish the [second]({{ site.baseurl }}/javascript-array-accessor-methods/) and third parts of this series which will consider the accessor and iteration methods.

**Update (15-09-2016):** [Part 2 - JavaScript Array Accessor Methods]({{ site.baseurl }}/javascript-array-accessor-methods/)

We have nine mutator methods in JavaScript and we shall go over each one in turn. Let's begin:

## Array.pop()

The `pop()` method removes the **last** element in an array and returns that element.

Here's a simple example:

```javascript
var cities = ["London", "Lagos", "New York", "Accra"];

var lastCity = cities.pop();

console.log(cities); //["London", "Lagos", "New York"]
{'fraction_non_alphanumeric': 0.10209424083769633, 'fraction_numerical': 0.002804786836200449, 'mean_word_length': 4.949944382647386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7580042', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1351, 'n_words': 636}