I recently read about ES6 const
keyword and I can understand its importance when having something like this:
(function(){
const PI = 3.14;
PI = 3.15; // Uncaught TypeError: Assignment to constant variable
})();
So, nobody can change my PI
variable.
The misunderstanding I have is that I don't understand in which situation the use of const
with objects can make sense (other than preventing people to do myObj = newValue;
).
(function(){
const obj = {a:1 ,b: 2, c:3};
//obj = {x:7 , y:8, z: 9}
//This is good
//TypeError: Assignment to constant variable.
obj.a=7; obj.b=8 ; obj.c=9;
console.log(obj); //outputs: {a: 7, b: 8, c: 9}
})();
So when declaring an object: when should I say: Now I must declare my object with const
?
const
assures that variabletemp1
won't have any other object's Binding."? – Already