What is the difference between let and var?
Scoping rules
The main difference is scoping rules. Variables declared by var
keyword are scoped to the immediate function body (hence the function scope) while let
variables are scoped to the immediate enclosing block denoted by { }
(hence the block scope).
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();
What is the difference between let and var in a multiple variable definition?
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. var
s 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.
What is the difference between `let` and `var` in Swift?
The let
keyword defines a constant:
let theAnswer = 42
The theAnswer
cannot be changed afterwards. This is why anything weak
can't be written using let
. They need to change during runtime and you must be using var
instead.
The var
defines an ordinary variable.
What is interesting:
The value of a constant doesn’t need to be known at compile time, but you must assign the value exactly once.
Another strange feature:
You can use almost any character you like for constant and variable
names, including Unicode characters:
let = "dogcow"
Excerpts From: Apple Inc. “The Swift Programming Language.” iBooks. https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewBook?id=881256329
Community Wiki
Because comments are asking for adding other facts to the answer, converting this to community wiki answer. Feel free edit the answer to make it better.
Difference between var and let
In the foo
function the b
variable is not accessible outside of the if
statement as well as the c
variable is not accessible outside of the while
.
The reason for this is that let
declared variables are block scoped.
For example the following log(b)
will result in b is undefined
:
function foo() {
var a = 1;
if (a >= 1) {
let b = 2;
while (b < 5) {
let c = b*2;
b++;
console.log(a + b);
}
}
console.log(b);
}
What is the difference between var and let in Typescript?
var
declaration is function scoped and let
declaration is block scoped.
See https://basarat.gitbooks.io/typescript/content/docs/let.html for more details.
What is the difference between let and var?
Scoping rules
The main difference is scoping rules. Variables declared by var
keyword are scoped to the immediate function body (hence the function scope) while let
variables are scoped to the immediate enclosing block denoted by { }
(hence the block scope).
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();
Related Topics
Bootstrap Close Responsive Menu "On Click"
JavaScript Get Element by Name
How to Remove a Style Added With .Css() Function
Accessing a CSS Custom Property (Aka CSS Variable) Through JavaScript
How to Change/Remove CSS Classes Definitions At Runtime
How to Get the Position of Text Within an Element
How to Update Placeholder Color Using JavaScript
Textarea That Can Do Syntax Highlighting on the Fly
React.Js: Set Innerhtml VS Dangerouslysetinnerhtml
Best Way to Detect That Html5 ≪Canvas≫ Is Not Supported
Html5 Local Storage Fallback Solutions
Detecting the System Dpi/Ppi from Js/Css
How to Transform Black into Any Given Color Using Only CSS Filters
Only Detect Click Event on Pseudo-Element