hamburger

What is the Difference Between Var and Let in JavaScript?

By BYJU'S Exam Prep

Updated on: September 25th, 2023

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.

Download Complete Digital Logic Formula Notes PDF

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 JavaScript Difference between Java and Core Java
Difference Between OLTP and OLAP Difference Between Recruitment and Selection
Difference Between Greedy Method and Dynamic Programming Difference Between Structure and union
Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium