DEV Community

Cover image for Difference between const, var and let in JS
Sakshi
Sakshi

Posted on • Edited on

1 1

Difference between const, var and let in JS

Difference on the basis of Scope, Redeclaration, Hoisting and problem with it.

var

Scope - Global/functional scope
Redeclaration - can be redeclared
Hoisting - var variables are hoisted to the top of their scope and initialized with a value of undefined.
Problem - can give you bugs more easily

let

Scope - Block scoped
Redeclaration - can be updated, but can't be redeclared
Hoisting - ust like var, let declarations are hoisted to the top. Unlike var which is initialized as undefined, the let keyword is not initialized. So if you try to use a let variable before declaration, you'll get a Reference Error.

const

Scope - const declarations can only be accessed within the block they were declared.
Redeclaration - cannot be updated or re-declared
Hoisting - const declarations are hoisted to the top but are not initialized

Find example here

Source - https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay