What is the Difference Between Var and Let in JavaScript?

By Priyanshu Vaish|Updated : June 20th, 2022

Difference Between Var and Let: Both var and let are useful for declaration in JavaScript, but there is a significant difference between var and let in JavaScript. Here, the let keyword is block-scoped while the var keyword is function scoped. The major difference between var and let keyword in JavaScript is not in the syntax used but rather differ in the semantics, which is discussed in the following article.

We can also state that any variable we declare with var stays more defined throughout any program (compared to the let keyword). The variable declared using the var keyword is used as a normal variable, but the variable declared using the let keyword is blocked scope.

Table of Content

Difference Between Let and Var

The table given below shows the difference between var and let keywords in JavaScript. The difference between var and let keyword in JavaScript is made on the basis of declaration, initialization, scope etc.

Key Differences Between Let and Var

Var

Let

In var, the scope is within the function only. 

In let, all the block have their different scope. 

The var declared variable can be redeclared. 

The let declared variable could not be redeclared otherwise, it throws the error. 

The var declared variable can be reassigned. 

The var declared variable can be reassigned. 

The syntax of var is:

var varname = value.

The syntax of var is:

let varname = value

The var is the feature of ECMAScript1.

The let is the feature of ES6.

What is Var in JavaScript?

Before knowing the difference between var and let, let’s understand var. The var is the keyword used in JavaScript for the purpose of insertion of the value of CSS variables. A variable that stays declared with this keyword stays defined throughout a given program.

The scope of the var variable can be local as well as global, which is as follows:

When the var variable count is declared outside the function. For example:

var count;

function maximum()

{

//code

}

In this, the scope of the var variable count is global, and it means it can be accessed from any other function.

When the var variable is declared inside the function. For example:

function maximum()

{

var count;

//code

}

In this, the scope of the var variable count is local, and it means it cannot be accessed from any other function.

What is Let in JavaScript?

Before knowing the difference between var and let, let’s understand let. The let is also the keyword similar to the var keyword, but there are some restrictions as compared to the var keyword in JavaScript. The let is also used in JavaScript for the purpose of creating the block scope variables.

The var variables can easily enhance the readability of the code, and it decreases the overall chances of running the programming error. Any variable we declare using the let keyword stays limited only to the block-scoped.

Check out some important topics related to the difference between Let and Var in the table provided below:

Difference Between Java and JavaScriptDifference between Java and Core Java
Difference Between OLTP and OLAPDifference Between Recruitment and Selection
Difference Between Greedy Method and Dynamic ProgrammingDifference Between Structure and union

Comments

write a comment

FAQs on Difference Between Var and Let in JavaScript

  • The difference between var and let keyword in JavaScript is that the var variable declared is treated as a normal variable, whereas the let keyword declared is treated as block-scoped, which means it can access within the block.

  • The let keyword in JavaScript allows you to declare a variable limited in scope to the block, expression or statement on which it is used. On the other hand, the var keyword in JavaScript defines a variable locally or globally to an entire function regardless of block scope.

  • Between the var and let keyword in JavaScript, the var keyword is faster, and the let keyword is slower while executing the code or program. The var declared variables are redeclared in the same function.

  • The primary difference between var and let is that the scope of a let variable declared is limited to the block of the code in which it is being declared, while a variable defined using the var keyword has the global scope irrespective of the block.

  • We use the let variable, not var, because the let variable keyword will be updated, but it cannot redeclare. This is because both the var and let keyword are treated as different variables since they both have different scopes. 

Follow us for latest updates