DEV Community

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

Posted on • Updated on

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/

Top comments (0)