In javascript, if I create a const array, I can still modify the object the variable points to:
// The const declaration creates a read-only reference to a value.
// It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
const x = [1,2,3];
x.push(4);
console.log(x);
x=55 // But this is illegal and will error
console.log(x);
Is there a way to make the elements in an array immutable as well? Similar to something like const int* const x; in C?
You can use Object.freeze to prevent an object (or array) from being changed:
const x = [1, 2, 3];
Object.freeze(x);
x.push(4); // This will throw an exception
objects frozen with Object.freeze() are made immutable.
Here are the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
Example:
const x = [1,2,3];
Object.freeze(x);