DEV Community

Discussion on: Javascript var, let and const

Collapse
 
chiubaca profile image
Alex Chiu

const till you can't is my rule. Simple and works well for me :)

Collapse
 
calvintwr profile image
calvintwr

A good starting thought, except you will run out of generic namespaces like key or name very soon 😂. So the thinking behind “should I const it?”, should be whether is it generic? If so, use let over const.

Collapse
 
chiubaca profile image
Alex Chiu

my other convention is to never allow generic namespaces haha . My code is scattered with long descriptive variable names.

Thread Thread
 
calvintwr profile image
calvintwr • Edited

Actually so are mine. I tend to be a little verbose.

However, I'm quite drawn by Ryan Dahl's (guy behind Node and Deno) style.

So nowadays I'm a little bit of both.

The other trick I learnt is that your eyes can read text with missing characters in-between, as long as the start and the end characters remains the same. So you can start to shorten using this trick.

var nvrAllow, gnrcNmspaces

Collapse
 
mohsenalyafei profile image
Mohsen Alyafei

Agree.

Collapse
 
nagwan profile image
nagwan sami Smiley Code • Edited

i loved the rhythm 😃
a huge portion in the community agrees on the same,
but i believe in making it more flexible as for some use cases variables are made to be updated, and for newbies, I am not sure if it is easy to know when you cannot form the early start,
but after all, i totally agree

Collapse
 
chiubaca profile image
Alex Chiu • Edited

I agree it's weird learning curve for newbies. I also enforce this convention with ESLint no-const-assign . This means that whenever I try to reassign a const, my linter will remind me to convert it to a let.

see the following - eslint.org/docs/rules/no-const-assign

Thread Thread
 
nagwan profile image
nagwan sami Smiley Code

oh🤩,
i have not tried that before,
it looks handy,
thanks for sharing🙏