4 ms·
const can be used to denote that a reference to the variable is being passed around and should not change for the lifetime of a scope. function f() {
by bmeck 11y ago
const can be used to denote that a reference to the variable is being passed around and should not change for the lifetime of a scope.
function f() {
const queue = [];
consumer(queue);
return queue;
}
It could be important to note that removing const has benign effect, but that it is there to ensure a programmer does not change the value of queue between `consumer(queue)` and `return queue`. Easily reasoned about, but extra safeguards from introducing bugs is always nice.
- digi_owl 11y agoI guess you can say that they are there to allow the compiler to check the intent of the programmer.
- EugeneOZ 11y agoIronically enough, but your example shows where const is shouldn't be used as guarantee of immutability: https://t.co/JIQozGe7Md https://t.co/JIQozGe7Md
- agumonkey 11y agoIsn't that a typical "corner case" in the understanding of dynamic languages bindings ? I think I've seen the same question pop about python. The array binding is constant, its content isn't.
- EugeneOZ 11y agoif value of constant can be changed, it's not constant. In my understanding. And by Wikipedia: http://en.wikipedia.org/wiki/Constant_(computer_programming) http://en.wikipedia.org/wiki/Constant_(computer_programming)
- agumonkey 11y agoThat's typical computer lingo / semantic issue. the `const` mean shallow/pointer constant, not deep/structurally constant.
- bmeck 11y agoconst is not about immutability in JS.
- thomasfoster96 11y agoI'm not sure I quite understand your point - while it's true that the variable called queue won't be reassigned, const would still allow elements and properties to be added to queue.
- bmeck 11y agoconst is not about immutability in JS.
- thomasfoster96 11y agoI know that - but the previous comment made it sound as though it did mean immutability.