I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
Const can be updated if it's an object or array. This is fine, for example:
constfoo=[];foo.push("hello");
So const is kinda-sorta-const, depending where you learned the word "constant". You can't redeclare it or make it point at anything else, but you can modify it if it's a container for other things.
Hello, World! I'm jzombie, a passionate software developer with a knack for problem-solving and a love for open-source. I believe in the power of code to change the world and make our lives easier.
Const gives you a constant memory reference. You can't reassign the memory reference, but any properties can be [re]assigned. The constant value assumption is only for primitive types.
I'm a coder who has worn a lot of hats, from individual contributor to lead engineer to "CTO" (yes, in quotes, make of that what you will!). I've a lot to learn and hopefully some to share as well.
It's not a reference to specific memory. The thing pointed to by a const variable can and often will move locations in memory (for example if you keep pushing onto an array, the engine will eventually have to allocate a new, bigger block of memory for it). It's just a constant reference to a specific value.
Hello, World! I'm jzombie, a passionate software developer with a knack for problem-solving and a love for open-source. I believe in the power of code to change the world and make our lives easier.
I am a Software Engineer proficient in Web development and have experience in various technologies. Inspired to automate the boring stuff, solve challenges, and innovate new digital solutions.
I am a Software Engineer proficient in Web development and have experience in various technologies. Inspired to automate the boring stuff, solve challenges, and innovate new digital solutions.
"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. "
I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
Whatever the technical explanation, it's added confusion for people new to programming in Javascript that something can be called "constant" but can be different depending where it's accessed in a function!
The expectation of a "constant" probably comes from the C/C++ const type qualifier.
"JavaScript is most despised because it isn’t some other language. If you are good in some other language and you have to program in an environment that only supports JavaScript, then you are forced to use JavaScript, and that is annoying. Most people in that situation don’t even bother to learn JavaScript first, and then they are surprised when JavaScript turns out to have significant differences from the some other language they would rather be using, and that those differences matter."
Douglas Crockford, JavaScript - The Good Parts, 2008; p.2:
JavaScript has its share of warts but it doesn't help when people coerce familiar mental models onto it that don't align with its actual behaviour (which extends to class-based object-orientation). JavaScript is its own thing.
Unfortunately beginners are often told to think of a "variable" as a box that holds a value (probably based on the value in a register idea). But the const confusion demonstrates that the "sticky note with a name which gets moved from one value to the next" mental model would probably be superior.
but can be different depending where it's accessed in a function!
I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
That expectation is because "const" isn't a word except as a truncation of "constant". There's nothing you can assume about it coming from another language or coming to Javascript as your first language that doesn't hinge on the word, "constant".
Coming from other languages I was holding the same prejudice, a source of continual annoyance.
Until I finally realized it meant "constant binding", not "constant value".
All of a sudden from that perspective the behaviour is entirely consistent.
A lot of things start to make sense when one takes into account that Brendan Eich was hired for "doing Scheme in the browser" but then had to comply with the "diktat from upper engineering management was that the language must 'look like Java'."
We also have Java to blame for null entering into the language. One bottom-most value (undefined) ought to be enough.
I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
But I imagine that the whole primitive values are immutable thing was established much, much earlier—leading to a situation where creating a "constant binding" was easy and relatively cheap in terms of run-time performance.
Facilities for creating immutable objects exist (Object.freeze() which is only shallow) but come with run-time overhead.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Const can be updated if it's an object or array. This is fine, for example:
So
const
is kinda-sorta-const, depending where you learned the word "constant". You can't redeclare it or make it point at anything else, but you can modify it if it's a container for other things.Const gives you a constant memory reference. You can't reassign the memory reference, but any properties can be [re]assigned. The constant value assumption is only for primitive types.
To freeze objects: developer.mozilla.org/en-US/docs/W...
It's not a reference to specific memory. The thing pointed to by a const variable can and often will move locations in memory (for example if you keep pushing onto an array, the engine will eventually have to allocate a new, bigger block of memory for it). It's just a constant reference to a specific
value
.Thanks for the clarification.
I'll check on this. Thanks for the feedback
Thanks for this information, I will learn it and update it
Too confusing an explanation...
All JavaScript primitive values are immutable to begin with—so once the binding is immutable nothing can change for primitive values.
So with
let
andvar
primitive values don't actually mutate but reassignment mutates the binding to a different value instead.1) MDN:
const
:"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. "2) MDN: JavaScript data types and data structures
"All types except objects define immutable values (that is, values which can't be changed)"
Whatever the technical explanation, it's added confusion for people new to programming in Javascript that something can be called "constant" but can be different depending where it's accessed in a function!
It's not called a "constant", it's a
const
declaration.The expectation of a "constant" probably comes from the C/C++ const type qualifier.
"JavaScript is most despised because it isn’t some other language. If you are good in some other language and you have to program in an environment that only supports JavaScript, then you are forced to use JavaScript, and that is annoying. Most people in that situation don’t even bother to learn JavaScript first, and then they are surprised when JavaScript turns out to have significant differences from the some other language they would rather be using, and that those differences matter."
Douglas Crockford, JavaScript - The Good Parts, 2008; p.2:
JavaScript has its share of warts but it doesn't help when people coerce familiar mental models onto it that don't align with its actual behaviour (which extends to class-based object-orientation). JavaScript is its own thing.
Unfortunately beginners are often told to think of a "variable" as a box that holds a value (probably based on the value in a register idea). But the
const
confusion demonstrates that the "sticky note with a name which gets moved from one value to the next" mental model would probably be superior.😕
That expectation is because "const" isn't a word except as a truncation of "constant". There's nothing you can assume about it coming from another language or coming to Javascript as your first language that doesn't hinge on the word, "constant".
Coming from other languages I was holding the same prejudice, a source of continual annoyance.
Until I finally realized it meant "constant binding", not "constant value".
All of a sudden from that perspective the behaviour is entirely consistent.
A lot of things start to make sense when one takes into account that Brendan Eich was hired for "doing Scheme in the browser" but then had to comply with the "diktat from upper engineering management was that the language must 'look like Java'."
We also have Java to blame for
null
entering into the language. One bottom-most value (undefined
) ought to be enough.Wasn't
const
added waaaay later though, like in ~2010 or something?ES2015 (ES6) actually.
But I imagine that the whole primitive values are immutable thing was established much, much earlier—leading to a situation where creating a "constant binding" was easy and relatively cheap in terms of run-time performance.
Facilities for creating immutable objects exist (
Object.freeze()
which is only shallow) but come with run-time overhead.