Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/basic-javascript/56bbb991ad1ed5201cd392cd.md
T
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

82 lines
1.5 KiB
Markdown

---
id: 56bbb991ad1ed5201cd392cd
title: Manipulate Arrays With shift Method
challengeType: 1
forumTopicId: 18238
dashedName: manipulate-arrays-with-shift
---
# --description--
`pop()` always removes the last element of an array. What if you want to remove the first?
That's where `.shift()` comes in. It works just like `.pop()`, except it removes the first element instead of the last.
Example:
```js
const ourArray = ["Stimpson", "J", ["cat"]];
const removedFromOurArray = ourArray.shift();
```
`removedFromOurArray` would have a value of the string `Stimpson`, and `ourArray` would have `["J", ["cat"]]`.
# --instructions--
Use the `.shift()` function to remove the first item from `myArray` and assign the "shifted off" value to a new variable, `removedFromMyArray`.
# --hints--
`myArray` should now equal `[["dog", 3]]`.
```js
assert(
(function (d) {
if (d[0][0] == 'dog' && d[0][1] === 3 && d[1] == undefined) {
return true;
} else {
return false;
}
})(myArray)
);
```
`removedFromMyArray` should contain `["John", 23]`.
```js
assert(
(function (d) {
if (
d[0] == 'John' &&
d[1] === 23 &&
typeof removedFromMyArray === 'object'
) {
return true;
} else {
return false;
}
})(removedFromMyArray)
);
```
# --seed--
## --seed-contents--
```js
// Setup
const myArray = [["John", 23], ["dog", 3]];
// Only change code below this line
```
# --solutions--
```js
const myArray = [["John", 23], ["dog", 3]];
// Only change code below this line
const removedFromMyArray = myArray.shift();
```