2018-09-30 23:01:58 +01:00
---
id: 56bbb991ad1ed5201cd392cc
title: Manipulate Arrays With pop()
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cRbVZAB'
2019-07-31 11:32:23 -07:00
forumTopicId: 18236
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
Another way to change the data in an array is with the < code > .pop()< / code > function.
< code > .pop()< / code > is used to "pop" a value off of the end of an array. We can store this "popped off" value by assigning it to a variable. In other words, < code > .pop()< / code > removes the last element from an array and returns that element.
Any type of entry can be "popped" off of an array - numbers, strings, even nested arrays.
2019-05-17 06:20:30 -07:00
```js
var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();
console.log(oneDown); // Returns 6
console.log(threeArr); // Returns [1, 4]
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
Use the < code > .pop()< / code > function to remove the last item from < code > myArray< / code > , assigning the "popped off" value to < code > removedFromMyArray< / code > .
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: < code > myArray</ code > should only contain < code > [["John", 23]]</ code > .
2019-07-13 00:07:53 -07:00
testString: assert((function(d){if(d[0][0] == 'John' & & d[0][1] === 23 & & d[1] == undefined){return true;}else{return false;}})(myArray));
2019-11-27 02:57:38 -08:00
- text: You should use < code > pop()</ code > on < code > myArray</ code > .
2019-07-13 00:07:53 -07:00
testString: assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
2018-10-20 21:02:47 +03:00
- text: < code > removedFromMyArray</ code > should only contain < code > ["cat", 2]</ code > .
2019-07-13 00:07:53 -07:00
testString: assert((function(d){if(d[0] == 'cat' & & d[1] === 2 & & d[2] == undefined){return true;}else{return false;}})(removedFromMyArray));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Setup
var myArray = [["John", 23], ["cat", 2]];
2020-03-02 23:18:30 -08:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
var removedFromMyArray;
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
(function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
var myArray = [["John", 23], ["cat", 2]];
var removedFromMyArray = myArray.pop();
```
< / section >