Image for post
Image for post

I’m going to try to explain why a const first approach is and should remain standard practice in JavaScript ES6+ going forward as well as the benefits. Many JavaScript elites are tangled with masses of misconceptions which skew how we believe the language should behave.

I advocate for learning the damn thing before trying to coerce it. Let’s start with the ELEPHANT of misconceptions…

Yes const is Immutable

const is an immutable primitive value or an immutable reference

Read that again, and again and again until you understand that the “reference object” !== “reference” . It’s the reference that is immutable not the object in reference. Mutability is at the core of how JavaScript works as an object based language. I don’t care how much experience, merits and fame someone claims to have if they don’t understand this, they don’t understand JavaScript as a paradigm and are likely conflating it with statically typed OOP principles.

If const was to make the “reference object” immutable it would only be practical for the things statically typed OOP languages use const for such as: STATIC_VALUES and config objects.

You can’t refactor const to let but you can refactor let to const

If you change a const to let it has zero impact with the exception of not knowing what values and references are to be mutated. But if you attempt to change all let values to const you may run into errors which is exactly what you want for refactoring. The aim of the game is to reduce mutable values and mutable object references.

const and let together should be immediate indications of assigned vs reassigned

Using const and let together behaves as metadata for healthy code. More information is always ideal. With let and const you are immediately told what is to change and what remains. With just let it’s a paper trail.

Separation of Concerns

Two things to consider:

  • What if you want a frozen value with a mutable reference?
  • What if you want a partially frozen value with an immutable reference.

For an object based architecture, it makes sense that const and Object.freeze/ seal are separate aspects of the interface.

Because const does not freeze its object values we can reference object based things like Arrays, Functions, Object literals etc practically. Mutation in JavaScript is a double edge sword you will NEVER stop it entirely so grow up and embrace the magic.

const forces you to think differently…

How do I simplify my expressions?

What happens from there?

You simplify your logic and resent your old immediate sloppy approaches.

You start using more ternary statements as well as AND and OR operators, you end up converting most of your let declarations to const as you refactor.

Whatever, I’ll continue to use var

Image for post
Image for post
My reaction

var is not only mutable, it’s re-declarable. So you can do

var a = ‘apple’; var a = ‘banana’;

I know you’re a very clever developer and you like the fact that var can be accessed outside of a block scope. Don’t do it, it’s harder to read in the long run.

Do not use var under no circumstances, unless you must support ES5 and below or play about within a dev console.

Yea but const is not really immutable homie?

Once again motherf**ker; const makes a value immutable or a reference immutable. For object immutability in JavaScript, you can use:

  • const
  • Object.freeze
  • Object.seal

freeze and seal are shallow, but like everything in JavaScript you are only given the bare-bones to build more complex functionality if needed (i.e. looping and freezing nested children). In conjunction with freeze and seal you can not achieve immutability using let.

Image for post
Image for post
This is what happens when you use let when a value does not need to be reassigned

So there you have it! Every logical reason to use const along side with let in the object based, dynamically typed, prototype language called JavaScript which differs to the necessities of OOP based, statically typed languages.

PS. A lot of the problems we have in JavaScript are due to the one-size fits all mentality. Don’t fall for the trap.

Bring the noise ;-)

Written by

Julien Etienne: Front End Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store