2018-09-30 23:01:58 +01:00
---
id: 56bbb991ad1ed5201cd392ce
title: Manipulate Arrays With unshift()
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/ckNDESv'
2019-07-31 11:32:23 -07:00
forumTopicId: 18239
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
Not only can you < code > shift< / code > elements off of the beginning of an array, you can also < code > unshift< / code > elements to the beginning of an array i.e. add elements in front of the array.
< code > .unshift()< / code > works exactly like < code > .push()< / code > , but instead of adding the element at the end of the array, < code > unshift()< / code > adds the element at the beginning of the array.
2020-03-25 08:07:13 -07:00
Example:
```js
var ourArray = ["Stimpson", "J", "cat"];
ourArray.shift(); // ourArray now equals ["J", "cat"]
ourArray.unshift("Happy");
// ourArray now equals ["Happy", "J", "cat"]
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
Add < code > ["Paul",35]< / code > to the beginning of the < code > myArray< / code > variable using < code > unshift()< / code > .
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
2018-10-20 21:02:47 +03:00
- text: < code > myArray</ code > should now have [["Paul", 35], ["dog", 3]].
2019-07-13 00:07:53 -07:00
testString: 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 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// Setup
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
2020-03-02 23:18:30 -08:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
(function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray);
2018-09-30 23:01:58 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
var myArray = [["John", 23], ["dog", 3]];
myArray.shift();
myArray.unshift(["Paul", 35]);
```
< / section >