2018-10-10 22:03:03 +00:00
---
id: 56bbb991ad1ed5201cd392ce
2021-02-06 04:42:36 +00:00
title: Manipulate Arrays With unshift()
2018-10-10 22:03:03 +00:00
challengeType: 1
2020-04-29 10:29:13 +00:00
videoUrl: 'https://scrimba.com/c/ckNDESv'
forumTopicId: 18239
2021-01-13 02:31:00 +00:00
dashedName: manipulate-arrays-with-unshift
2018-10-10 22:03:03 +00:00
---
2020-12-16 07:37:30 +00:00
# --description--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
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.
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`.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.
Example:
```js
var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift(); // ourArray now equals ["J", "cat"]
ourArray.unshift("Happy");
// ourArray now equals ["Happy", "J", "cat"]
```
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --instructions--
2020-04-29 10:29:13 +00:00
2021-02-06 04:42:36 +00:00
Add `["Paul",35]` to the beginning of the `myArray` variable using `unshift()` .
2018-10-10 22:03:03 +00:00
2020-12-16 07:37:30 +00:00
# --hints--
2018-10-10 22:03:03 +00:00
2021-02-06 04:42:36 +00:00
`myArray` should now have \[["Paul", 35], ["dog", 3]].
2018-10-10 22:03:03 +00:00
```js
2020-12-16 07:37:30 +00:00
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-10-10 22:03:03 +00:00
```
2021-01-13 02:31:00 +00:00
# --seed--
## --after-user-code--
```js
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
```
## --seed-contents--
```js
// Setup
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
// Only change code below this line
```
2020-12-16 07:37:30 +00:00
# --solutions--
2020-04-29 10:29:13 +00:00
2021-01-13 02:31:00 +00:00
```js
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
myArray.unshift(["Paul", 35]);
```