0

After Const array variable is declared, but each of its elements can still be modified. I want to freeze it, so no one other than me can modify it. I may modify the value (unfreeze it) afterwards. How to do it?


Const num= [1,2,3,4];
num[0]=5;
console.log(num); //output:[5,2,3,4]

If I do not allow this numto be modified by anyone, must I only use "Object.freeze" to do it? After some researching and experimenting, I found that "Object.freeze" is quite troublesome where it can not be unfrozen after freezing it. How can I do it?

Freezes the array num so no one can modify it except the coders. But it still allows the coders to use whatever methods to somehow unfreeze the array,and do something like push a value to this array.

0 Answers0