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
?
When to use JavaScript const? As a general rule, always declare a variable with const unless you know that the value will change. Use const when you declare: A new Array.
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.
In JavaScript, `const` means that the identifier can't be reassigned. (Not to be confused with immutable values. Unlike true immutable datatypes such as those produced by Immutable. js and Mori, a `const` object can have properties mutated.)
The const keyword allows you to specify whether or not a variable is modifiable. You can use const to prevent modifications to variables and const pointers and const references prevent changing the data pointed to (or referenced).
it is a common misconception around the web, CONST
doesn't creates immutable variables instead it creates immutable binding.
eg.
const temp1 = 1; temp1 = 2 //error thrown here.
But
temp1.temp = 3 // no error here. Valid JS code as per ES6
so const
creates a binding to that particular object. const assures that variable temp1
won't have any other object's Binding.
Now coming to Object
. we can get immutable feature with Object
by using Object.freeze
const temp3 = Object.freeze( {a:3,b:4}) temp3.a = 2 // it wont update the value of a, it still have 3 temp3.c = 6 // still valid but wont change the object
According to ES6-Features.org, constants are used to make "variables which cannot be re-assigned new content".
The const
keyword makes a variable itself immutable, not its assigned content. When the content is an object, this means the object itself can still be altered.
Therefore, it's possible to change the content of the object that is declared with const
variable, but you cannot assign a new object to a const
variable.
You are still allowed to add new attributes to your object.
const myVar = "someValue"; const myObj = {"name": "nameValue", "age": 14} console.log(myVar); //someValue console.log(myObj.name); //nameValue myObj.name = "newNameValue"; console.log(myObj.name); //newNameValue myObj.someNewAttr = "newAttrValue"; console.log(myObj.someNewAttr); //newAttrValue myObj = {"newNameAttr": "newNameValue"}; //Uncaught TypeError: Assignment to constant variable. console.log(myObj.newNameAttr); myVar = "newValue"; //Uncaught TypeError: Assignment to constant variable. console.log(myVar);
You can also see on this fiddle: https://jsfiddle.net/am2cbb00/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With