DEV Community

var, let & const in JavaScript

Kunal Tiwari on February 27, 2021

ES2015(ES6) was released long back and one of the features that came with ES6 is the addition of let and const, another way for variable declaratio...
Collapse
 
lakshayne profile image
Lakshay Sharma

i just googled it and it says something like this and i am kind of confused now🤔🤔🤔

The main difference between let and var is that scope of a variable defined with let is limited to the block in which it is declared while variable declared with var has the global scope. So we can say that var is rather a keyword which defines a variable globally regardless of block scope.

Collapse
 
kunalt96 profile image
Kunal Tiwari

Yes, that what I wrote above. Yes, let has block scope and var has functional scope. You are mixing two things here and that is what confusing you. Whenever any variables declared outside function, outside blocks in globally - either be let or var, it will have full global scope

Collapse
 
lakshayne profile image
Lakshay Sharma

ok than you very much 👍👍

Thread Thread
 
kunalt96 profile image
Kunal Tiwari

Always welcome buddy :)

Collapse
 
standoge profile image
Stanley Melgar

Good post!

Collapse
 
kunalt96 profile image
Kunal Tiwari

Glad you liked it, Please do share with your connections as well

Collapse
 
curlmuhi profile image
Ekta Maurya

thank you for this ,it clears my confusion as i'm a newbie.

Collapse
 
kunalt96 profile image
Kunal Tiwari

Thanks a lot. Glad you liked it. Js is all easy, you can start with some.good tutorials online, things will be good.

Collapse
 
ra1nbow1 profile image
Matvey Romanov

Simple and clear

Collapse
 
kunalt96 profile image
Kunal Tiwari

Glad you liked it, Please do share with your devlopers friends and community

Collapse
 
anirudh711 profile image
Anirudh Madhavan

Main problem with var is the concept of 'hoisting'. It is better to stick with const and let.
More on w3schools.com/js/js_hoisting.asp

Collapse
 
kunalt96 profile image
Kunal Tiwari • Edited

I really appreciate it. Hoisting is a seprate concepts in JavaScript related to the var keyword. I didn't put that in the article here. But was expecting some discussion on this :). Will definitely talk about it on next article. Glad you read it. Let and const are additions but they are the best for variables declaration in js due to these certain side effects