DEV Community


Discussion on: Confused by JavaScript's const? Me too!

ahferroin7 profile image
Austin S. Hemmelgarn

Other than the readability aspect (which is indeed big), there are a few other bnefits to using const that many people don't think about:

  • It tells the JS engine that the reference won't change. This sounds useless, but it has the potential to allow for some optimizations that would not otherwise be possible. I'm not sure whether any of the big JS implementations actually take advantage of this or not, but it's still worth considering.
  • Some IDE's, as well as some code checking tools, can be configured to complain if they see an indentifier declared with const on the left side of an assignment expression, even if the assignment would be to a property of the referenced object.

Honestly, if I know for certain that something shouldn't change, I use const, period. Yes, it can lead to confusion, but that confusion is usually less significant than properly annotating that something shouldn't change.