Question regarding CONST

Hello campers,

When passing the following algorithm DNA Pairing I found out the following:

const at = ["A", "T"];

at.reverse();

console.log(at); // -> ["T", "A"]

How is that possible? Why this happens? :exploding_head:

When a variable declared with const is an object (arrays are objects), it can be mutated but it cannot be reassigned.

at.reverse();

will work.

at = ["T", "A"];

will not.

Yeees, and I found out this on MDN:

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. For instance, in the case where the content is an object, this means the object’s contents (e.g., its properties) can be altered.

Now it makes much more sense for me.

I’m glad you got it figured out. Happy coding!

1 Like