DEV Community

You either 'let', 'var' or keep 'const' in JavaScript

Lody G Mtui on June 12, 2022

Difference between var, let and const In JavaScript, variables are assigned using three main keywords/ways which are:- var let const....
Collapse
 
moopet profile image
Ben Sinclair

And const can neither be re-declared nor updated.

Const can be updated if it's an object or array. This is fine, for example:

const foo = [];

foo.push("hello");
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
jzombie profile image
jzombie

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

Collapse
 
lodyne profile image
Lody G Mtui

I'll check on this. Thanks for the feedback

Collapse
 
mistval profile image
Randall • Edited

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.

Thread Thread
 
jzombie profile image
jzombie

Thanks for the clarification.

Collapse
 
lodyne profile image
Lody G Mtui

Thanks for this information, I will learn it and update it

Collapse
 
peerreynders profile image
peerreynders

Too confusing an explanation...

const means that the name-to-value binding is immutable—not the value.

All JavaScript primitive values are immutable to begin with—so once the binding is immutable nothing can change for primitive values.

So with let and var 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)"

Collapse
 
moopet profile image
Ben Sinclair

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!

Thread Thread
 
peerreynders profile image
peerreynders • Edited

it's added confusion for people new to programming in Javascript that something can be called "constant".

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.

but can be different depending where it's accessed in a function!

😕

Thread Thread
 
moopet profile image
Ben Sinclair

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

Thread Thread
 
peerreynders profile image
peerreynders • Edited

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.

Thread Thread
 
moopet profile image
Ben Sinclair

Wasn't const added waaaay later though, like in ~2010 or something?

Thread Thread
 
peerreynders profile image
peerreynders

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.

Collapse
 
steveknoblock profile image
Steve Knoblock

The computer science definition of constant existed for forty years or more before JavaScript decided to use the same term for something different than most computer languages and in mathematics.