PDA

View Full Version : What is the main difference between var and let in JavaScript?



jhonsmith123
11-27-2017, 02:44 AM
What is the main difference between var and let in JavaScript?

Lebar.123
11-27-2017, 09:31 AM
The difference is scoping. var is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block. Both are global if outside any block.

Also, variables declared with let are not accessible before they are declared in their enclosing block. As seen in the demo, this will throw a ReferenceError exception.

saurabh mathur
11-28-2017, 02:31 AM
VAR- the JavaScript variables statement are used to declare the variable and we can initialize the value of that variable.

LET- this refers to the local variable in a block scope. It is also similar to var and we can optionally initialize the variable.

arianagrand
12-04-2017, 06:46 AM
Scoping makes the difference. var's scope is function level but let is scoped in its enclosing block, that is ({}).

davidsmith21
07-20-2018, 07:14 AM
Var is scoped to the nearest function block and let is scoped to the nearest enclosing block, which can be smaller than a function block.

rohit
08-20-2018, 02:33 AM
var and let are both used for function declaration in javascript but the difference between them is that var is function scoped and let is block scoped.
It can be said that a variable declared with var is defined throughout the program as compared to let.

seojesica
09-18-2018, 05:56 AM
var and let are both used for function declaration in javascript but the difference between them is that var is function scoped and let is block scoped.
It can be said that a variable declared with var is defined throughout the program as compared to let.



Yes, I completely agree with you.