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
---
2020-11-27 19:02:05 +01:00
# --description--
Another way to change the data in an array is with the `.pop()` function.
`.pop()` 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, `.pop()` removes the last element from an array and returns that element.
2018-09-30 23:01:58 +01:00
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]
```
2020-11-27 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Use the `.pop()` function to remove the last item from `myArray` , assigning the "popped off" value to `removedFromMyArray` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`myArray` should only contain `[["John", 23]]` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
(function (d) {
if (d[0][0] == 'John' & & d[0][1] === 23 & & d[1] == undefined) {
return true;
} else {
return false;
}
})(myArray)
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should use `pop()` on `myArray` .
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`removedFromMyArray` should only contain `["cat", 2]` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
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
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --after-user-code--
2018-09-30 23:01:58 +01:00
```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
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
// Setup
var myArray = [["John", 23], ["cat", 2]];
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
// Only change code below this line
var removedFromMyArray;
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
var myArray = [["John", 23], ["cat", 2]];
var removedFromMyArray = myArray.pop();
```