Why we don’t use var in EC6 onwards

Abhishek
1 min readJan 11, 2022

--

I am getting my hands dirty with ES6 and it has introduced let and constant keywords, coming from Java background, I instantly got the meaning of what constant is and poor kid I was, I thought let keyword is just fancied version of var, BUT IT’s NOT!

If I write two lines of code

on line 1, I write : var brand = ‘tesla’;

on line 2, I write: var brand = ‘porche’;

It’s completely VALID! and why someone would have problem with that?

It’s because if you have large file with good amount of LOC’s then there is a very good chance that another developer can introduce variable with same name somewhere in that file and it result in logical error which wouldn’t be caught until something blows up in production or you have 100% code coverage via Unit Tests.

For the same reason, ES6 introduced keyword let.

If I re-write two lines of code

on line 1 I write : let brand = ‘tesla’;

on line 2 I write: let brand = ‘porche’; // THIS LINE WILL ERROR OUT

I am now thinking how smart were the developers of ES6, they came up with something that prevents logical bugs, not often you see that, even though this has existed in other languages for ages.

That was my Day 4 /100 of coding with Front-end, see you in next chapter!

Click here to go to Day 5

--

--

Abhishek
Abhishek

Responses (1)