2018-09-30 22:01:58 +00:00
---
id: 56bbb991ad1ed5201cd392ce
title: Manipulate Arrays With unshift()
challengeType: 1
2019-02-14 17:24:02 +00:00
videoUrl: 'https://scrimba.com/c/ckNDESv'
2019-07-31 18:32:23 +00:00
forumTopicId: 18239
2021-01-13 02:31:00 +00:00
dashedName: manipulate-arrays-with-unshift
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
Not only can you `shift` elements off of the beginning of an array, you can also `unshift` elements to the beginning of an array i.e. add elements in front of the array.
`.unshift()` works exactly like `.push()` , but instead of adding the element at the end of the array, `unshift()` adds the element at the beginning of the array.
2020-03-25 15:07:13 +00:00
Example:
```js
var ourArray = ["Stimpson", "J", "cat"];
2021-03-03 00:12:12 +00:00
ourArray.shift();
2020-03-25 15:07:13 +00:00
ourArray.unshift("Happy");
```
2021-03-03 00:12:12 +00:00
After the `shift` , `ourArray` would have the value `["J", "cat"]` . After the `unshift` , `ourArray` would have the value `["Happy", "J", "cat"]` .
2020-11-27 18:02:05 +00:00
# --instructions--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
Add `["Paul",35]` to the beginning of the `myArray` variable using `unshift()` .
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
2021-02-17 19:24:53 +00:00
`myArray` should now have `[["Paul", 35], ["dog", 3]]` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(
(function (d) {
if (
typeof d[0] === 'object' & &
d[0][0] == 'Paul' & &
d[0][1] === 35 & &
d[1][0] != undefined & &
d[1][0] == 'dog' & &
d[1][1] != undefined & &
d[1][1] == 3
) {
return true;
} else {
return false;
}
})(myArray)
);
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--
```js
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
```
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
```js
// Setup
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
2020-03-03 07:18:30 +00:00
// Only change code below this line
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
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
myArray.unshift(["Paul", 35]);
```