const numbers = [1,2,3,4,5]
let luckyNum = numbers.pop()
What will be the value of numbers?
Hint: numbers is stored in a constant not a variable
const numbers = [1,2,3,4,5]
let luckyNum = numbers.pop()
What will be the value of numbers?
Hint: numbers is stored in a constant not a variable
From the MDN you can read:
Constants are block-scoped, much like variables defined using the let statement. The value of a constant cannot change through reassignment, and it can't be redeclared.
In you particular case the numbers
variable holds a reference to the array and that reference will remain "constant", not the array itself. As you can check on next example:
const numbers = [1,2,3,4,5];
let luckyNum = numbers.pop();
console.log("luckyNum:", luckyNum, "numbers:", numbers);
// Now, next line will trhow an error, because we are
// trying to do a reassingment on a const variable:
numbers = [];
In the particular case you are interested, you can use Object.freeze() to prohibit changes on an array
of primitives values:
const numbers = [1, 2, 3, 4, 5];
Object.freeze(numbers);
// Now, next line will thrown an error.
let luckyNum = numbers.pop();
console.log("luckyNum:", luckyNum, "numbers:", numbers);
I Hope this clarifies your doubts.
Even though it's a const, it will be modified and the new value will be [1,2,3,4].