Reputation: 704
Naturally I assumed the "const," keyword meant the value of a variable could not change, and when it comes to integers and strings that seems to be the case. However, today I was watching a video and somebody type the following
const my_list = [];
my_list.push(someValue);
I was surprised to find out that this kind of code actually works, as I was under the assumption that the list would be constant. So my question is: Why? What advantage does declaring the list as constant have if you are just going to change it anyways?
Upvotes: 2
Views: 4734
Reputation: 96
When you assign variable with string/number/boolean variable contains the value of string/number/boolean, but then you assign variable with object or array variable contains the address of that object/array in 'memory'.Then you push something in array the value of variable is still same address of array in memory , and that is why you not get error
Upvotes: 3