I always thought that const variables in JavaScript would be constants... like "variables that cannot change".
So I always assumed the benefit of using const over let/var would be something like decreased resources usage.
But after a quick test it looked like a const pretty much acts like a let-variable. It's block scoped and can be modified... at least in my browser (Firefox 47.0).
Here's what I did:
const FOO = [0,1,2] FOO[0] = 11 console.log(FOO) // > [11, 1, 2] And that leads to my question: What is the point of using const? I mean the word is longer, lot's of people don't know of const and it straight up seems to act like a normal variable. So why would I give the illusion that it is not? Why don't we just keep writing constants as let/var with the name in all-caps?
FOO(i.e.FOO = 23) becauseFOOisconst. But because objects are mutable in JavaScript, you can still mutate the object itself. Related questions: stackoverflow.com/q/31205975/218196, stackoverflow.com/q/22308071/218196, stackoverflow.com/q/26015747/218196Object.freeze()orseal. Memory locations cannot be replaced when using const, i.e It will throw error at that time.constvariable or avarvariable? It provides the ability to make code cleaner. I would think it does nothing but provide a benefit to the language as a whole.StringandBool? You could certainly argue that by that it would become more apparent what a variable does. But in my opinion a complete lack of data types is a huge advantage.