DEV Community

Michael Akanji
Michael Akanji

Posted on

When to use LET or CONST

Hello there,

Wooooow, you are interested in my opinion. I would like to get to know yours too in the comment section below :-D.

I have gone through numerous lines of javascript code from different coders. And I have seen the different use case of let and const, not conventional I would say.

So, I thought to myself maybe I should share the way I address which to use whenever I am coding, How I choose whether to use let or const.

How do I choose?

Here are my recommendations;

  • Use let only on primitive values that are liable to be mutated.

e.g

let balance = 475
Enter fullscreen mode Exit fullscreen mode
  • const as it's apparent, should be used for primitive values that you want to make immutable and its identifier should be in UPPER_CASE.

e.g

const PI = 3.14 // do you remember this guy from primary math...
Enter fullscreen mode Exit fullscreen mode
  • And lastly, always use const for objects.

e.g

// this? , you don't want to lose the reference to your object, right?
// which also encourage explicit operations on your object properties

const EmptyObj = {}
Enter fullscreen mode Exit fullscreen mode

I hope this my little opinion is helpful. Once again, I wish to read your opinion in the comment section of this post. It might open my eyes to a different perspective.

Do you enjoy the read?

Top comments (5)

Collapse
 
curtisfenner profile image
Curtis Fenner

I don't think the object case is different from the primitive case.

I always default to using const and just use let if I really need to reassign a variable (like iterating in a for loop or accumulating something).

There are lots of times where it's reasonable to re-assign variables that hold objects.

Collapse
 
matscode profile image
Michael Akanji • Edited

I don't recommend having the mindset to reassign an object variable.

Reasons

If it is an object of the same typeOf instance... I would recommend you rather update the objects properties value than reassign the variable object itself.

It would make your code obfuscated if you use let for an object because you want to change it later. In my opinion, I think it is a bad practice, no offense.

I wouldn't want to be reading through a code and I suddenly see a User object variable become Fruit object variable. The maintainability of such code is questionable...

So how can you prevent such thing from happening? I believe it is by declaraing variable with object value as a const...

This way, you can loop and update all object property but not the variable itself..

I don't know if this is helpful though...

Collapse
 
diek profile image
diek

If the object is the same instance than before, it is not better to update it persé, it depends, the garbage collector will delete unused pointers so no pain in reassign it, depending of the application making an update/copy method can be so much elaborated, even more if you are receiving info fetching api requests.
I do the same as Curtis, always const until i need to reassign the variable.

Collapse
 
curtisfenner profile image
Curtis Fenner

1) If you use TypeScript (and there's really not much reason not to anymore), changing the type is something the compiler will reject -- the problem of not knowing the type of a variable is not a problem of assignment but the lack of static analysis in JavaScript.

2) mutating objects is filled with danger, because you don't know who else has a handle on the object. Invoking immutable/pure functions will necessitate assignment in loops.

Thread Thread
 
matscode profile image
Michael Akanji

I see where you are coming from...

You have 2 points. :-D

It seems to appear like we have a common point.

However, here, the post is just about pure JS.

It's been nice debating with you.. :raise-hand: