Skip to main content

Command Palette

Search for a command to run...

JavaScript Variable Declarations: var vs let vs const.

Published
1 min readView as Markdown
JavaScript Variable Declarations: var vs let vs const.
S

Hey,

I'm Kamruzaman Shojib and I'm a front-end developer. I love to talk about tech mostly I'll be writing about JavaScript and trying to help the community.

Hello everyone, today we are going to discuss JavaScript Variable Declarations and the difference between them.

There are three ways to declare variables in JavaScript var let and const .

So, What's the difference between and why do we even bother?

Var

If we declare a variable with var it gets hosited on top of its containing scope or we say this is global scope. And also variable delared with var it is reassingable.

Let

Now let's declare a variable with let.

Now we have solved the problem of hoisting but another problem is remaining and that is verable reassigning.

Const

Last but not least let's see when we declare a variable with const.

So, Now we have solved both problem of hoisting and reassigning.

Conclusion

so, now the of which one to use totally depends on your specific requirements. The use of Use var sparingly, as it has been largely replaced by let and const in JavaScript. You should use let when you need variables with reassignable values within a block scope. For variables that should not be reassigned, you should use const for maintainable robust code.

Thanks, everyone hanging in there till the last ❤️!!

More from this blog

Shojib's blog

10 posts