Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript
communitycreator

Difference between var, let, and const keyword in JavaScript

Harsh Jain

In JavaScript, the keywords var, let, and const are a bit confusing. So, in this shot, we will look at the differences between these keywords.

Scope

var let const
Variables declared with var are in the function scope. Variables declared as let are in the block scope. Variables declared as const are in the block scope.

Look at the below code snippets to understand this.

function f1()
{
    var a=10;
}
console.log(a)
Scope difference between var, let, and const keyword

Explanation:

  • In the var tab, when you will run the code, you will see an error as a is not defined because var variables are only accessible in the function scope.
  • In the let tab, when you run the code, you will see an error as i is not defined because let variables are only accessible within the block they are declared.
  • In the const tab, when you run the code, you will see that x was defined under the block was accessible, but when you try to access x outside that block, you will get an error.

Hoisting

Hoisting means that you can define a variable before its declaration.

var let const
Allowed Not allowed Not allowed

Take a look at the code snippets below to further understand this.

x = 8; 

console.log(x);

var x;
Hoisting difference between var, let, and const keyword

Explanation:

  • In the var tab, when you run the code, you will see there is no error and that we can declare var variables after defining them.

  • In the let tab, when you run the code, you will get an error as let variables do not support hoisting.

  • In the const tab, when you run the code, you will get an error as const variables do not support hoisting.

Reassign the value

To reassign a value is to reassign the value of a variable.

var let const
Allowed Allowed Not allowed

Take a look at the code snippets below to understand this.

var v1 = 1;

v1 = 30;

console.log(v1);
Reassign difference between var, let, and const keyword

Explanation:

  • In the var and let tab, when you run the code, you will see that there is no error and we can define new values to the var and let variables.

  • In the const tab, when you run the code, you will get an error as a const variables value cannot be reassigned.

Redeclaration of the variable

The redeclaration of a variable means that you can declare the variable again.

var let const
Allowed Not allowed Not allowed

Take a look at the code snippets below to understand this.

var v1 = 1;

var v1 = 30;

console.log(v1);
Hoisting difference between var, let, and const keyword

Explanation:

  • In the var tab, when you run the code, you will see that there is no error as we allowed to declare the same variable again.

  • In the let and the const tabs, when you run the code, you will get an error as the let and const variables do not allow you to redeclare them again.

RELATED TAGS

javascript
communitycreator
RELATED COURSES

View all Courses

Keep Exploring