2018-10-12 15:37:13 -04:00
---
title: Mutate an Array Declared with const
---
2019-07-24 00:59:27 -07:00
# Mutate an Array Declared with const
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
Reassign the values of the `const` variable `s` using various element assignment.
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
* You can change array values on `const` like you can with `var` or `let` .
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 1
2018-10-12 15:37:13 -04:00
* To access array value use array[index]
2019-07-24 00:59:27 -07:00
---
## Solutions
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
```javascript
2019-07-24 00:59:27 -07:00
const s = [5, 7, 2];
function editInPlace() {
"use strict";
s[0] = 2;
s[1] = 5;
s[2] = 7;
}
editInPlace();
2018-10-12 15:37:13 -04:00
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
Trying to reassign a read-only `const` variable will throw an error, but by using various element assignment you can access and change the value of an array just like you would with `let` or `var` .
#### Relevant Links
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const' target = '_blank' rel = 'nofollow' > const</ a >
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array' target = '_blank' rel = 'nofollow' > Array</ a >
2019-07-24 00:59:27 -07:00
< / details >