2018-09-30 22:01:58 +00:00
---
id: 56bbb991ad1ed5201cd392cc
title: Manipulate Arrays With pop()
challengeType: 1
2019-02-14 17:24:02 +00:00
videoUrl: 'https://scrimba.com/c/cRbVZAB'
2019-07-31 18:32:23 +00:00
forumTopicId: 18236
2021-01-13 02:31:00 +00:00
dashedName: manipulate-arrays-with-pop
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
Another way to change the data in an array is with the `.pop()` function.
2021-03-03 00:12:12 +00:00
`.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.
2020-11-27 18:02:05 +00:00
2021-03-03 00:12:12 +00:00
Any type of entry can be popped off of an array - numbers, strings, even nested arrays.
2019-05-17 13:20:30 +00:00
```js
2021-10-25 16:55:58 +00:00
const threeArr = [1, 4, 6];
const oneDown = threeArr.pop();
2021-03-03 00:12:12 +00:00
console.log(oneDown);
console.log(threeArr);
2019-05-17 13:20:30 +00:00
```
2021-03-03 00:12:12 +00:00
The first `console.log` will display the value `6` , and the second will display the value `[1, 4]` .
2020-11-27 18:02:05 +00:00
# --instructions--
2018-09-30 22:01:58 +00:00
2021-10-25 16:55:58 +00:00
Use the `.pop()` function to remove the last item from `myArray` and assign the popped off value to a new variable, `removedFromMyArray` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`myArray` should only contain `[["John", 23]]` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00: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 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
You should use `pop()` on `myArray` .
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
assert(/removedFromMyArray\s*=\s*myArray\s*.\s*pop\s*(\s*)/.test(code));
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`removedFromMyArray` should only contain `["cat", 2]` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(
(function (d) {
if (d[0] == 'cat' & & d[1] === 2 & & d[2] == undefined) {
return true;
} else {
return false;
}
})(removedFromMyArray)
);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --after-user-code--
2018-09-30 22:01:58 +00:00
```js
2021-10-25 16:55:58 +00:00
if (typeof removedFromMyArray !== 'undefined') (function(y, z){return 'myArray = ' + JSON.stringify(y) + ' & removedFromMyArray = ' + JSON.stringify(z);})(myArray, removedFromMyArray);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
// Setup
2021-10-25 16:55:58 +00:00
const myArray = [["John", 23], ["cat", 2]];
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
// Only change code below this line
2021-10-25 16:55:58 +00:00
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2021-10-25 16:55:58 +00:00
const myArray = [["John", 23], ["cat", 2]];
const removedFromMyArray = myArray.pop();
2018-09-30 22:01:58 +00:00
```