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.
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.