Final variables

Hi there!

The const keyword is kind of a bad design:

https://jamie.build/const

More and more great developers seem to acknowledge that:



const everywhere in modern JS is semantically weird and ugly (it's like hammering the wrong word again and again). It's not what you would expect in 99% of other programming languages out there. If brings confusion to new developers as well as experienced developers coming from other languages.

So I was wondering: how could we fix that? I mean, declaring variable is the basics of what is a programming language, yet JavaScript fails at that.

We could maybe introduce a true "no reassignment syntax" (final variables) that would look like that:

let :foo = 'bar'; // can't reassign foo here in contrast to let foo = 'bar';

It would also works elsewhere:

for (let :value of iterable) {
}
function (:param1, :param2) {
}

What do you think? I am not sure if this is a good idea or not, and I am not sure if there is a better character to use than the colon.

I think it's more elegant than using this wordy and semantically misleading const.

Thank you!

That some people believe const was a mistake doesn't mean that's a universally held opinion, either outside or within TC39. I think const is wonderful, and not a mistake, and I think it should be used everywhere :-)

"Weird" and "ugly" are subjective preferences that are also not universally shared - on this topic or any other.

const means "constant reference", not "constant value", and I think that educating people of that is a much more viable option than introducing another way to declare variables. const has shipped on the web (for 5+ years now) and can never be removed, so devs can't ever avoid needing to learn it anyways.

6 Likes

And to add to that, the JVM world is the exception in calling their constant references final - most other languages use const. Even the K combinator in lambda calculus, x => ignored => x, is typically named const in most functional languages that implement it (like Haskell), and the result, const x (equivalent to ignored => x), is itself more conceptually a constant reference rather than a constant value.

1 Like

I appreciate your answer Jordan! Thank you! :heart_eyes:

It's really hard to discuss stuff like that, because it's really about how we "feel" or "believe", not really about "real metrics". Is const really useful in a codebase or is it just wishful thinking? I have programmed in many languages in my career and I never had a single problem related to variable reassignment.

Yes in JavaScript const means "constant reference", not "constant value", but in computer science semantics, it's always a "constant value" like constant PI = 3.14. The keyword "final" would maybe better catch the idea of "constant reference".

let final foo = 'bar';

function (final foo) {
}

So we can use const to semantically signify a "constant value"?

Anyway, I hope one day I could ditch let and const altogether and do something like that in JS:

string foo = 'bar';

:sunglasses:

Thanks Isiah! I didn't know that! :blush:

Here is an interesting Twitter thread about what's wrong with const:

So to summarize:

You complain about const because it's confusing to people from other languages, and then you propose let :foo ? Ask one of those folks on twitter what they think your syntax means. I guess no one will even get close to the actual meaning.

Also ... what is that new syntax supposed to do? A mere replacement for const? In that case you are a bit late to the party .... Or is it supposed to provide "real immutability"? If so, how? Freezing objects? Immutable References that do not support [[Set]] operations? What happens if you pass a :frozen to another function? Is it immutability there too?

Also mixed with object destructuring you can get full immutability also for objects with const, so I don't really get the point of those ranting:

const { name, age } = retrieveUser();

2 Likes