Difference between let and var in JavaScript?

Difference between let and var in JavaScript?

As we know how to declare a variable in JavaScript . There are two ways to declare a variable Such as var and let. Know, the main question is what is the Difference between let and var in JavaScript. Here is your answer.

A variable in JavaScript is an identifier that provides a storage location, which we use to store a data value. The data stored at a particular location in memory. That we accessed by using the variable name.

In JavaScript programs, a script will have to store the bits of information temporarily. It needs to do its work. It can store this data in variables. In other words, A variable is a symbolic name that we give to a location in the computer memory. Where we store the value, which we use in the program. The value of the variable can change during the execution of the program. It may take different values at different times during the execution of the program.

For example:

var Price = 100;

Let Keyword

Let keyword, used to declare a variable. With let, the scope of a variable has limited scope to the block where it declared. However, It can be only available inside the block where you declare it.

For Example:

let world = "hey hi";  
let times = 5;  
if (times > 3) {  
   let hello = "Say Hello world";   

As a Result:

Var Keyword

Var keyword used to declare a variable like let do. It has function scoped, while the variable declared with var has global scope in the program.

For Example.

var world = "hey hi";  
var times = 5;  
if (times > 3) {  
   var world = "Say Hello world  ";   
console.log(world) //Output: Say Hello world

As a Result:

Difference between let and var in JavaScript?

1.The var keyword used in JavaScript.The let keyword was added in ES6 (ES 2015) version of JavaScript.
2.It has global scope throughout the program.It is limited to block scope, where it is defined.
3.It can be declared globally and can be accessed globally out side the block.It can be declared globally, However cannot be accessed globally. However, it can accessed within the block where it declared.
4.Variable declared with var keyword can be re-declared and updated in the same scope.
Example:function varGreeter(){ var a = 10; var a = 20; //a is replaced console.log(a); } varGreeter();
Variable declared with let keyword can be updated, However not re-declared.
Example:function varGreeter(){ let a = 10; let a = 20; //SyntaxError: //Identifier ‘a’ has already been declared console.log(a); } varGreeter();
5.It is hoisted.
console.log(a); //undefined.
//Due to hoisting
var a = 2;
It is not hoisted.
console.log(e); // ReferenceError: //b is not defined
let e = 3;

Re-Declaring JavaScript Variable

We also can re-create the variable without changing its value. For example, if we re-declare myAge will still have the same value 24 after execution of these statements.

For Example

<!DOCTYPE html>

<h2>JavaScript Variables</h2>

<p>If you re-declare a JavaScript variable, it will not lose its value.</p>

<p id="demo"></p>

var myAge = "24";
var myAge;
document.getElementById("demo").innerHTML = myAge;


As a Result:

JavaScript Variables
If you re-declare a JavaScript variable, it will not lose its value.


Leave a Reply

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