DEV Community

Cover image for Javascript var, let and const

Javascript var, let and const

nagwan sami on July 18, 2020

I hate intros but we must have one 🤷 Before 2015 javascript developers used to use var as the keyword to declare variables, and life was easy, but...
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
 
nagwan profile image
nagwan sami • 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

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

Collapse
 
mohsenalyafei profile image
Mohsen Alyafei

Agree.

Collapse
 
ayahosnym profile image
Aya Hosny

Wow,that's amazing Nagwan, thanks a lot
I want to know more about npm,npx, yarn if you write about this it will be so nice

Collapse
 
nagwan profile image
nagwan sami

thanks for your feedback,
sure as soon as i can

Collapse
 
moatasemebrahim profile image
MoatasemEbrahim

Way to go Nagwan 🎉🎉
It's very useful as you have used some examples that we find in our daily work.

I think it will be great if you can give us a brief about the data types in javascript.

Hope the best for you and waiting you coming interesting articles

Collapse
 
nagwan profile image
nagwan sami • Edited

thank u Moatasem for the feedback,
and for the data types article, i will as soon as i can

Collapse
 
richytong profile image
Richard Tong

Great demonstration of var vs let/const. I remember getting asked in a couple interviews about the difference, I wish I could reproduce an example as clear as this one back then.

Collapse
 
nagwan profile image
nagwan sami • Edited

chances never end😉,
just be ready for the next one💪

if want any topic to be discussed here u can suggest it

Collapse
 
calvintwr profile image
calvintwr • Edited

And not forgetting the best that let has done is to solve nested looping:

for(let i=0; i<100; i++) { for(let i=0; i<500; I++) {}}

Where before you would need to be using i, j, k... until if you have complicated loops with conditionals, you won’t even know if it has conflicted.

Collapse
 
nagwan profile image
nagwan sami • Edited

this is one of the most common interview questions,
thanks for mentioning that,
i wanted to add this section, but i wanted the article to be as concise as I could,
thank u

Collapse
 
m7mdhendawy profile image
Mohammed Fathi

Good job 👍👍

Collapse
 
nagwan profile image
nagwan sami

thank you

Collapse
 
loaideveloper profile image
Loai-developer

Nice article and direct to the point,
If you write about closures I will be grateful
Keep it up

Collapse
 
nagwan profile image
nagwan sami

it is gonna be the next article,
thanks for your feedback

Collapse
 
sirmohammedahmed profile image
Mohamed Ahmed

Great

Collapse
 
nagwan profile image
nagwan sami

thanks

Collapse
 
mohsenalyafei profile image
Mohsen Alyafei

Nice article.
Thanks

Collapse
 
nagwan profile image
nagwan sami

hope it helps,
thank u for the feedback

Collapse
 
tefa profile image
Mohamed Atef

A simple and clear explanation, thank you for this article

Collapse
 
nagwan profile image
nagwan sami

thanks for the feedback

Collapse
 
saiedarabyads profile image
Mohamed Saied

Great article Nagwan Keep it up
Very straight forward and got the point easily.
Focus on scoping part to elaborate the concept and the difference is brilliant
👏👏👏

Collapse
 
nagwan profile image
nagwan sami

thanks, Saied, I am really grateful for your kind words 🥰

Collapse
 
ahmedmahmoud profile image
Ahmed Mahmoud • Edited

Clear and detailed explanation, you make it like a story
Great! Keep going..

Collapse
 
nagwan profile image
nagwan sami

thank you

Collapse
 
marwanahmed profile image
Marwan Ahmed

great article