Home » What is the difference between let, const and var in javascript.
person using macbook-diggobi

What is the difference between let, const and var in javascript.

This post shows with example of the differences between let, const and var in javascript. Javascript ES6 is a time saver for programmers

A lot of shiny new features came out with ES6 . And now, since it’s 2022, it’s assumed that a lot of JavaScript developers have become familiar with and have started using these features and it has made development more easier when we use these javascript concepts.

ES6 has presumably made things easier in 2022

Software engineers

The inclusion of let and const, which can be used for variable declaration, is one of the improvements that arrived with ES6. What distinguishes them from regular old var, which we have been using, is the question. This article is for you if you’re still unclear on this.

So what is the difference between const , var and let in javascript

1. var

Var declarations dominated before the introduction of ES6. But there are problems with variables that are declared with var. It was required for new methods of declaring variables to appear for this reason. Let’s first learn more about var before we talk about those concerns.

Scope of var

Scope basically refers to the area in which these variables can be used. Var declarations can be function/locally or globally scoped.When a var variable is declared outside of a function, the scope is global. This means that any variable declared outside of a function block using the var keyword is accessible throughout the entire window.

When a variable is declared inside a function, it has a block scope. This indicates that it is accessible and limited to that block. Moreover variable is also redefinable.

example:




var fullName = "";

console.log({ fullName });
/**
The output of fullName 
here will be ""
**/

{
  var fullName = "Daim Dev";
/**
In this block fullName 
will be changed to Daim
Dev and moreover variables
is redefinable
**/
console.log({ fullName});
// the output will be Daim Dev
}

/**
The output of the fullName here will
will be Daim Dev
**/
console.log({ fullName });



2. let

Let is now the preferred method for declaring variables. It shouldn’t come as a surprise because it enhances var declarations. Additionally, it resolves the issue we just discussed with var. Let’s analyse the reasons for this.

List of points to be noted while working with let in javascript

  1. let has got functional/block scope
  2. let cannot be redeclared.

Look at the example below

example:




let fullName = "";

console.log({ fullName });
/**
The output of fullName 
here will be ""
**/

{
  let fullName = "Daim Dev";
/**
In this block fullName 
will be changed to Daim
Dev and moreover variables
is redefinable
**/
console.log({ fullName });
// the output will be Daim Dev
}

/**
The output of the fullName here will
will be ""
**/
console.log({ fullName });



3. const

Constant values are kept for variables specified using the const keyword. Let declarations and const declarations have some similarities

List of points to be noted while working with const in javascript

  1. const cannot be redeclared
  2. const cannot be reassigned

This indicates that within the scope of a variable declared with const, its value will not change. It cannot be changed or declared again. As a result, if we declare a variable with const, we cannot:

Look at the example below

example:




const fullName = "";

console.log({ fullName });
/**
The output of fullName 
here will be ""
**/

{
  const fullName = "Daim Dev";
/**
In this block fullName 
will be changed to Daim
Dev and moreover variables
is redefinable
**/
console.log({ fullName });
// the output will be Daim Dev
}

/**
The output of the fullName here will
will be ""
**/
console.log({ fullName });

const fullName = "Daim Dev"
// At this line compiler will
// throw following error : 
 
// Identifier 'fullName' has already
// been declared

Got any question or additions? Please watch the video below

For Video tutorial watch this video belo

More Reading

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *