Javascript - const object reference

Introduction

const declares an immutable variable but it does not make the value contained in the variable immutable.

In the following code obj is a constant, but the value it points to is mutable.

You can add a property to it but you cannot assign a different value to obj.

const obj = {}; 
obj.key = 42; 
console.log(obj.key); // 42 
obj = {}; // TypeError 

To make the value itself immutable, use the Object.freeze().

const obj = Object.freeze({}); 
obj.key = 42; 
console.log(obj);   // {} 

Object.freeze() is shallow and only freezes the properties of the object passed to it.

Only one level of properties of the object become immutable.