What s the difference between using let and var

0 votes
I've heard that it's described as a local variable, but I'm still not quite sure how it behaves differently than the var keyword.

What are the differences?. When should it be used instead of var?
Feb 17, 2022 in Java by Soham
• 9,700 points
447 views

1 answer to this question.

0 votes

The main difference is scoping rules wherein the variables declared by var keyword are scoped to the immediate function body while let variables are scoped to the immediate enclosing block denoted by { }.


function run() { 
var foo = "Foo"; 
let bar = "Bar"; 

console.log(foo, bar); // Foo Bar 
{ 
var moo = "Mooo" 
let baz = "Bazz"; 
console.log(moo, baz); // Mooo Bazz 
} 

console.log(moo); // Mooo 
console.log(baz); // ReferenceError 
} 

run();

The reason why the let keyword was introduced to the language was function scope is confusing and was one of the main sources of bugs in JavaScript.

var funcs = []; 
// let's create 3 functions 
for (var i = 0; i < 3; i++) { 
// and store them in funcs 
funcs[i] = function() { 
// each should log its value. 
console.log("My value: " + i); 
}; 

} 

for (var j = 0; j < 3; j++) { 
// and now let's run each one to see 
funcs[j](); 
}

My value: 3 was output to console each time funcs[j](); was invoked since anonymous functions were bound to the same variable.

People had to create immediately invoked functions to capture correct values from the loops but that was also hairy.

While variables declared with var keyword are hoisted which means they are accessible in their enclosing scope even before they are declared:
 

function run() { 
console.log(foo); // undefined 
var foo = "Foo"; 
console.log(foo); // Foo 
} 

run();

 

Do let variables are not initialized until their definition is evaluated. Accessing them before the initialization results in a ReferenceError. The variable is said to be in "temporal dead zone" from the start of the block until the initialization is processed.

function checkHoisting() { 
console.log(foo); // ReferenceError 
let foo = "Foo"; 
console.log(foo); // Foo 
} 

checkHoisting();

 
At the top level, let, unlike var, does not create a property on the global object:


var foo = "Foo"; // globally scoped 
let bar = "Bar"; // not allowed to be globally scoped 
console.log(window.foo); // Foo 
console.log(window.bar); // undefined

 

Finally, In strict mode, var will let you re-declare the same variable in the same scope while let raises a SyntaxError.


'use strict'; 
var foo = "foo1"; 
var foo = "foo2"; // No problem, 'foo1' is replaced with 'foo2'. 

let bar = "bar1"; 
let bar = "bar2"; // SyntaxError: Identifier 'bar' has already been declared
answered Feb 17, 2022 by Aditya
• 7,680 points

Related Questions In Java

0 votes
2 answers

What's the difference between @JoinColumn and mappedBy when using a JPA @OneToMany association?

JPA mapping annotation can be classified as ...READ MORE

answered Oct 28, 2020 in Java by bjjj
• 140 points
20,353 views
0 votes
1 answer

What's the difference between SoftReference and WeakReference in Java?

Weak references A weak reference, simply put, is a ...READ MORE

answered Jun 12, 2018 in Java by Rishabh
• 3,620 points
1,690 views
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,220 points
15,307 views
0 votes
1 answer

What is the difference between jdk and jre?

JRE: It stands for Java Runtime Environment. ...READ MORE

answered Apr 20, 2018 in Java by Akrati
• 3,190 points
1,695 views
0 votes
0 answers

What is the difference between "let" and "var"?

Although I've heard that it's referred to ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
386 views
0 votes
1 answer
0 votes
0 answers

Download mp4 files at once with jQuery via console

I am opening HTML pages in a ...READ MORE

Jul 19, 2022 in Web Development by gaurav
• 23,260 points
631 views
0 votes
1 answer

How Impala is fast compared to Hive in terms of query response?

Impala provides faster response as it uses MPP(massively ...READ MORE

answered Mar 21, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
1,894 views
0 votes
1 answer
0 votes
1 answer

Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core”

If you have a jstl.jar file while ...READ MORE

answered Feb 17, 2022 in Java by Aditya
• 7,680 points

edited Jun 22, 2023 by Khan Sarfaraz 23,469 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP