What are the Differences Between var, let, and const?

What are the Differences Between var, let, and const?

Ferenc Almasi β€’ Last updated 2023 April 19 β€’ Read time 1 min read
  • twitter
  • facebook
JavaScript

When no keyword is used, the variable will be assigned to the global object which is the window if we are in a browser. There are three keywords in JavaScript you can use for variable declarations. These are:

var
This is the default: it creates a function scoped variable which can be reassigned or redeclared later on.

let
This was introduced in ES6 and it is the preferred way over using var. It creates a block-scoped variable which can be reassigned but can’t be redeclared.

const
It was introduced in ES6 along with let. It also creates a block-scoped variable but this can’t be reassigned nor redeclared. const should be used when you don’t want to reassign a value later on.

It’s also good to mention that all declarations will be hoisted to the top of their scope.

var should be avoided if can, use let or const instead.

var vs let vs const in JavaScript
If you would like to see more Webtips, follow @flowforfrank

50 JavaScript Interview Questions

Resources:

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.