000
000

Reputation: 11

What is the difference between let and var in a multiple variable definition?

Syntax error:

let admin,
names = "Bob",
admin = names;

alert(admin);
Using the var variable is not recommended, but could such a definition cause an error? "var" No error:
var admin,
names = "Bob",
admin = names;

alert(admin);

Different syntax of the let variable (No error):

let admin, names;

names = "Bob";

admin = names;

alert(admin);
I've explored the difference between the var and let variable, but I don't fully understand the differences here.

What are the differences between the codes?

Upvotes: 1

Views: 1280

Answers (3)

Omkar Hande
Omkar Hande

Reputation: 81

It's simple! var and let are both used for variable declaration in typescript but the difference between them is that var is function scoped and let is block scoped. Let's see an example below.

// calling x after defining 
var x = 5; 
document.write(x, "\n"); 
  
// calling y after defining  
let y = 10; 
document.write(y, "\n"); 
  
// calling var z before defining will return undefined 
document.write(z, "\n"); 
var z = 2; 
  
// calling let a before defining will give error 
document.write(a); 
let a = 3; 

Upvotes: 0

Jörg W Mittag
Jörg W Mittag

Reputation: 369633

The difference is that you can declare a variable more than once with var, but not with let:

var v = 42;
var v;
console.log(v);

let l = 42;
let l;
// SyntaxError: Cannot declare a let variable twice: 'l'.

Actually, in your example, we get bitten even earlier by 14.3.1.1 Declarations and the Variable Statement – Let and Const Declarations – Static Semantics: Early Errors clause 2:

It is a Syntax Error if the BoundNames of BindingList contains any duplicate entries.

let l, l;
// SyntaxError: Cannot declare a let variable twice: 'l'.

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 371233

When you declare variables while using commas in a single statement, you're declaring a variable for each comma (plus the first one). For example:

let admin,
names = "Bob",
admin = names;

is equivalent to, separated out into 3 statements:

let admin;
let names = "Bob";
let admin = names;

But variables declared with let and const cannot be initialized more than once; in a given scope, there must be exactly one line that initializes them with let or const.

In contrast, variables declared with var do not have such a limitation. vars essentially get hoisted up to the top of the function. So

var foo = 5;
foo = 10;

is like

var foo;
foo = 5;
foo = 10;

and duplicate var declarations in the same scope aren't a problem, since they'll all refer to the same hoisted identifier at the top. That's why your second snippet doesn't throw an error.

Your third snippet doesn't throw because the admin and names are each initialized exactly once:

let admin, names;

is like

let admin;
let names;

which is fine. Assigning to a variable multiple times isn't an issue (as long as the variable isn't declared with const); it's just the initialization of a let variable must happen only exactly once.

Upvotes: 1

Related Questions