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?
my_list = ['some other array']. Read developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…const the_list = my_list, then the contents ofthe_listwould be updated as long as you'd usemy_list.push, but you would "split the timeline" by doingmy_list = my_list.concat(...)for example. Withconstthe latter would not be allowed.constdoesn't make the actual value immutable, it just means you can't re-assign something to the constant. It's beneficial because it reduces the risk of bugs and unintended side-effects.