2
hinst
2y

If you ever feel useless, remember that `const` exists in JavaScript

Comments
  • 7
    And why would that be useless?

    Its very useful.
  • 12
    The const keyword has one very specific purpose - namely preventing accidental reassignment - and it's doing great at it.
  • 1
    @kamen I’m curious. How does it do it exactly?
    I mean since it’s not compiled, it can only be checked at runtime and then throw an error or something.
    If it works like that, then I agree with OP: It’s useless.
  • 0
    Umm.. wut.
  • 0
    So useless! Let us keep using var! 🎉
  • 1
    Ok, maybe not entirely useless. But runtime enforced immutability is so far away from the usefulness of true immutability that it feels like an insult to call it "const".
  • 6
    if you ever feel useless: remember that this rant exists, which you read, scrolled into the comments of, and got pissed. Value your time.
  • 5
    @Lensflare Yes obviously. It's an interpreted language 🙄
  • 5
    Who cares if it happens at run time, it's like saying Typescript is useless because it happens at build time. Sure from a software perspective it's a little curious, but from a developer experience perspective it is absolutely fantastic.

    It's all about making programming safer, not the program.
  • 2
    Yes scripting languages so everything at run time... However the language constructs do not have to be checked only at run time. Your IDE can report an eye when you are modifying a constant.
  • 3
    I first use const, then let and rarely var. They all have a purpose, well, if you don't write shitty code.
  • 1
    @Lensflare
    const x = Object.freeze(3);
  • 0
    @melezorus34 yes, const prevents reassignment. Freeze prevents modification of the object.

    My criticism about the claimed usefulness of constants was not about that.

    It was about the fact that nothing prevents you from ignoring the declaration of something being a constant at development time.

    You will find out that you did a mistake when it manifests as a bug in production on the end user’s machine.

    This is the safety aspect of the usefulness of constants.

    There is also the performance aspect:
    When it’s enforced at runtime, it costs performance. Compile time enforcement enables optimizations and can improve performance.
  • 2
    @Lensflare i take your point with a grain of salt and add:

    If you have an IDE like geany and got so far to push PRODUCTION code with it, you got mad luck.

    Every fucking [javascript supporting] IDE ever shouts out "bro you just tried to re-assign on a const, that's cringe, you can't re-declare with let bro" and those are shouted as errors because they fucking are.

    Even if you don't have typescript, eslint, a CODE WRITING STYLE enforcerer will complain about it too.
  • 1
  • 2
    Adding to the point "when it's checked at runtime, it's useless"*:

    People write python code and push to production. (lmao no consts)
    People write lua code and upload to Steam Workshop.
    People write CSS and upload it to a site.

    In every one of them, it will always be a runtime error. (But css don't has const? any error thrown in css is in runtime, that was my point)
  • 2
    @Lensflare most dev tools will warn even if js is not compiled unless your using plain text editor with no tooling.
  • 1
    @Lensflare just because js is not compiled does not mean it cannot be checked using lint tools that have been around for decades.

    Thats not a language problem but dev environment problem.

    Like mentioned above, all interpreted languages can have the same problem unless you use some extra tool to test things.

    Adding const helps those tools a lot to know the intent.

    Without it the tools will not know the intent and cannot warn.
  • 0
    @Voxera yes, I see. Still, the usefulness of const then becomes more of a property of the IDE and not of the language itself. The original post was about "const in JavaScript".

    But I agree that in practice it‘s still more useful than I thought.
  • 3
    @Lensflare well, they cannot change js to be compiled so adding const was the next best thing.
Add Comment