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 in Java by Soham
• 8,730 points
58 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 by Aditya
• 7,280 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
16,266 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,003 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
7,315 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,170 points
914 views
0 votes
1 answer
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,103 views
0 votes
1 answer

How do I set variable if a specific package version is installed in CFEngine?

Here is what you can do.Just use packagesmatching to ...READ MORE

answered Jul 12, 2018 in Other DevOps Questions by Atul
• 10,230 points
481 views
0 votes
2 answers

Install postgreSQL on Ubuntu

Follow the below commands to install PostgreSQL (PSQL) ...READ MORE

answered Nov 12, 2020 in Database by Prachi
• 140 points
377 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 in Java by Aditya
• 7,280 points
17 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP