2018-10-10 18:03:03 -04:00
---
id: 587d7b87367417b2b2512b42
2021-02-06 04:42:36 +00:00
title: Mutate an Array Declared with const
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:13:35 +08:00
forumTopicId: 301206
2021-01-13 03:31:00 +01:00
dashedName: mutate-an-array-declared-with-const
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
The `const` declaration has many use cases in modern JavaScript.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Some developers prefer to assign all their variables using `const` by default, unless they know they will need to reassign the value. Only in that case, they use `let` .
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
However, it is important to understand that objects (including arrays and functions) assigned to a variable using `const` are still mutable. Using the `const` declaration only prevents reassignment of the variable identifier.
2020-08-04 15:13:35 +08:00
```js
const s = [5, 6, 7];
s = [1, 2, 3]; // throws error, trying to assign a const
s[2] = 45; // works just as it would with an array declared with var or let
console.log(s); // returns [5, 6, 45]
```
2021-02-06 04:42:36 +00:00
As you can see, you can mutate the object `[5, 6, 7]` itself and the variable `s` will still point to the altered array `[5, 6, 45]` . Like all arrays, the array elements in `s` are mutable, but because `const` was used, you cannot use the variable identifier `s` to point to a different array using the assignment operator.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
An array is declared as `const s = [5, 7, 2]` . Change the array to `[2, 5, 7]` using various element assignments.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should not replace `const` keyword.
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
(getUserInput) => assert(getUserInput('index').match(/const/g));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`s` should be a constant variable (by using `const` ).
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
(getUserInput) => assert(getUserInput('index').match(/const\s+s/g));
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should not change the original array declaration.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
(getUserInput) =>
assert(
getUserInput('index').match(
/const\s+s\s*=\s*\[\s*5\s*,\s*7\s*,\s*2\s*\]\s*;?/g
)
);
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`s` should be equal to `[2, 5, 7]` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert.deepEqual(s, [2, 5, 7]);
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:13:35 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
const s = [5, 7, 2];
function editInPlace() {
// Only change code below this line
// Using s = [2, 5, 7] would be invalid
// Only change code above this line
}
editInPlace();
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
const s = [5, 7, 2];
function editInPlace() {
s[0] = 2;
s[1] = 5;
s[2] = 7;
}
editInPlace();
```