ES6: Mutate an Array Declared with const - pulkitbhutani/My-FreeCodeCamp-Code GitHub Wiki
The const declaration has many use cases in modern JavaScript.
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.
"use strict";
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]
An array is declared as const s = [5, 7, 2]. Change the array to [2, 5, 7] using various element assignment.
const s = [5, 7, 2];
function editInPlace() {
"use strict";
// change code below this line
s[0]=2;
s[1]=5;
s[2]=7;
// s = [2, 5, 7]; <- this is invalid
// change code above this line
}
editInPlace();