let and var difference

Solutions on MaxInterview for let and var difference by the best coders in the world

showing results for - "let and var difference"
Raphael
03 Apr 2020
1var is function scoped and let is block scoped. Let's say you have:
2function understanding_var() {
3	if (1 == 1) {
4    	var x = 5;
5        console.log('the value of x inside the if statement is ' + x);
6    }
7    console.log(x);
8} 
9//output: the value of x inside the if statement is 5
10		  5
11
12function understanding_let() {
13	if (1 == 1) {
14    	let x = 5;
15        console.log('the value of x inside the if statement is ' + x);
16    }
17    console.log(x);
18} 
19//output: the value of x inside the if statement is 5
20		  Uncaught ReferenceError: x is not defined
21          
22var is defined throughout the entire function, even if it's inside the if 
23statement, but the scope of let is always within the curly braces, not outside
24it, even if the conditional statement is inside the function.
Luca
11 Jan 2019
1let a = 'hello'; // globally scoped
2var b = 'world'; // globally scoped
3console.log(window.a); // undefined
4console.log(window.b); // 'world'
5var a = 'hello';
6var a = 'world'; // No problem, 'hello' is replaced.
7let b = 'hello';
8let b = 'world'; // SyntaxError: Identifier 'b' has already been declared
Lilly
15 Apr 2017
1function run() {
2  var foo = "Foo";
3  let bar = "Bar";
4
5  console.log(foo, bar);
6
7  {
8    let baz = "Bazz";
9    console.log(baz);
10  }
11
12  console.log(baz); // ReferenceError
13}
14
15run();
Anton
30 Nov 2018
1// var is function-scoped, so redeclaring it in a block will cause its value outside the block to change as well:
2
3var one = 'one: declared outside the block';
4
5if (true === true) {
6  var one = 'one: declared inside the block'; // notice: we redeclare 'one' here
7  console.log(one); // prints 'one: declared inside the block'
8}
9
10console.log(one); // also prints 'one: declared inside the block', because the variable was redeclared in the 'if' block. The outer 'var' variable was therefore destroyed and replaced by inner var variable.
11
12// 'let' is block-scoped, so redeclaring a 'let' variable inside of a block creates a different 'let' variable with the same name whose scope is inside the block:
13
14let two = 'two: declared outside the block';
15
16if (true === true) {
17  let two = 'two: declared inside the block';
18  console.log(two); // prints 'two: declared inside the block'
19}
20
21console.log(two); // prints 'two: declared outside the block', because two declared inside the block is a separate variable. The 'let' variables are unrelated and therefore are unaffected by each other.
Ninon
19 Sep 2017
1/* DIFFERENCE BETWEEN LET AND VAR */
2
3//LET EXAMPLE
4{
5  let a = 123;
6};
7
8console.log(a); // undefined
9
10//VAR EXAMPLE
11{
12  var a = 123;
13};
14
15console.log(a); // 123
Antonella
22 Oct 2018
1/*
2
3Therefore, 
4  
5  var:
6    -can be declared outside any function to be used inside any function
7    -can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
8    -can be changed again and again anywhere
9  let:
10    -can be declared outside any function to be used inside any function
11    -if declared inside any function or any other {} which are of only if or if-else or switch etc. and can't be used anywhere inside the function and can be only used inside statement
12    - can be changed again and again only inside the statement in which they are made in
13    const:
14    -can be declared outside any function to be used inside any function
15    -can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
16    -cannot be changed again and agan anywhere, if tried to, will result in an error
17
18*/
19// var has function scope. (this variable can be accssed from anywhere inside function)
20// let & const has block scope.(this variable can not be accessed out of block. means outside of if else but inside of a function it can not be accessed. only inside that block we can access this variable)
21// https://www.youtube.com/watch?v=--Vh17ocC_s
22function func(){
23  if(true){
24    var A = 1;
25    let B = 2;
26  }
27  A++; // 2 --> ok, inside function scope
28  B++; // B is not defined --> not ok, outside of block scope
29  return A + B; // NaN --> B is not defined
30}
31//example 2
32var variable1 // declared using var
33const variable2 // declared using const
34
35myFunction1();
36
37function myFunction1() {
38  variable1 = "hello!";
39  console.log(variable1);
40  // "hello!"
41}
42
43myFunction2();
44
45function myFunction2() {
46  variable2 = "hello!";
47  // error: variable2 is a constant and can not be redifined
48}
49
50myFunction3(true);
51
52myFunction3(codition) {
53  if(condition) {
54    let variable3 = "helo!" // declared using const
55  }
56  variable3 = "hello!";
57  // error: variable3 is out of scope
58}
queries leading to this page
var vs cast vs letlet jswhere to use var and letexample diff betwenn let and vardifference between let and var and const in javascriptdifference between var and let jswhat is the difference between var and let in javascriptdiff between let and vardiference between var and letdifference between var const and let in javascriptdifferences between var o letdifferemce betwween let and varjs difference between let and varletin javascriptwhat is let vs vardifference between var and let in jsjavascript difference between let and vardifference between let var constlet vs var in jsuse var instead of let jswhat is the difference between variables created using let 2c var 2c and const 3f4 what is the difference between var and let 3fvar letdiffer betwen var and letdifference between a variable and letlet vs var vs 3dvar let and conts differencejs let vs varmain difference between let and vardifference between 22let 22 and 22var 22 3f 27 27 27difference of let or var in jsvar or let javascriptdiffrence between var and letdifferece between var and letsvar vs letdifference let var2 what are the major differences between let and var in javascript 3fvariables let vs varvar javascript 22 vs 27difference between var and let in jquerydiference var and letin java script will var and let do the same thingdifference between let an d var in jsdiff bw var and letjavascript let and var differencelet vs var in forwhat is the difference between var and let 3f 2alet vas vardiffrence between let and varedifference between let and var javascriptvar and let difference in javascriptjavascript should i use var or letdifference between using 22let 22 and 22var 22 3fdiffrent between var and letydifference betwwen let in and let ofwhat is different between let and varis let or var better in jsin javascript the main differences between let and var variable declarations arethe difference between a var and a letlet and var keyword differencejavascript why use let instead of varjs difference let and varjavascript let vs var differencewhat 27s the difference between let and var in jswhat is t difference between var and letdiffrenece between var and let in javascriptwhen to use var or let in jsdifference between let and let 21what should we use 3flet or vardifference between var let and conslet and var diff in javascriptwhen use var or letthe difference between var and let in jsdifference between const var and let javascriptwhat is the difference between var and let in js 3fvar let difference jsdifference between var and let in node jslet and var example in javascriptdifference between let and var 27javasript what is the difference betwen let and vardifference between let an vardifference between cont var and letjs diff between let and arrdifferences between var and let javascriptdifferencer between let and vardifference between let and var in javascript with exampledifference bwtween var and letdifferenve between let and var in javascriptwhy difference between let and vardifference between var and let keywordsdifference beween let 2c const 26 vardifference between 22let 22 and 22var 22 3fdiffernece between let and var exampleswhat 27s the difference between using let and var to declare a variable in es6 3fdifference between var let and varwhat is defferint bettwen let and vardifff between var and letjavascript difference let varwhat is the difference between let and var in jswhat is the difference between let and var 3fdifference between let and var 3fwhat is dif between var and letdifference b 2fw var and letlet var difference javascriptvar and letdifference between let and var keywordlet vs vardiffference between let and var in jswhats better to use in javascript var or letwhat is the difference between let and varjava script var vs letdifference between var let and javascriptin javascriptlet and var in javascriptlet and var difference jsdif between let vardiffernce between let and vardiffrence let varjavascript let or var 3fjavascript let vs functiondifference between using let and var in nodejswhats the diffrent between var and letwhat is the difference between e2 80 9cvar e2 80 9d and e2 80 9clet e2 80 9d in javascript 3flet or varnode js var vs letdifference betweeen var letdifference between let var javascriptdifference between let cont and varwhat is the differance between let and vardifference var et let4 difference between let and var in javascriptwhat is difference between letwhat is diff between var and letwhat are the differences between variables created using let 2c var 2c and const 3f 2a 3fdifference between let 2c var 3fwhat is difference between var and let in javascriptvar vs let differencewhat to use let vs var jswhat is the differnce between let and vardifference between const let and vardefrent between let and var in jswhat is the difference between let and var in javascriptdifference between let const and var javascriptdifference between const var and letdifference between let and normal variable in jslet vs var examplejs difference of let and varwhat 27s the difference between 22var 22 vs 22let 22 in javascript 3fjs difference let vardifference between var and let n jsdiff beetweeb vr and let in jsjavascript use let or vardifference between let and varthe difference between let and vardifference beteween var and letvar javascript vs letwhen to use var vs letjavascript different let and vardifference between let an cardifference in var and letthe diffrence between var and letdiffrence between let and varjavascript differences with let and varvar and let in javascriptdifference between let var in javascriptwhat is the difference bettween var and letdifference between let and var in javascriptvar vs let in jsjavascript scope difference let var explainedwaht is difference between var and letdifference between let const and var in javascriptduffreant between let or varwhat difference between let and varlet var difference jsjavascript use var or letvar vs 24var let in javascriptdifference between var const and letwhat is the difference between let or varjavascript difference between var and letdifference betweeen var and letshould i use let or var in javascriptlet and var difference in javascriptis it better to use let or varlet vs varrdiff between let and var in javascriptnode js let vs vardiff between let varthe difference between let and var in javascriptdifference btw var and letdifference between let or var in javascriptjavascript var vs let 27difference between var and let javascfiptis var and let the same thingdiff c3 a9rence var et let javascriptdifference between 22let 22 and 22var 22 3f 27 27diff bt let and varlet vs var jswhat 27s the difference between using let and var in javascriptjavascript var and letjavascript difference var letjs let or varif let and var keyworddifference between var and let javascriptdifference between const 2c let and var in jsare let and var samedifference between avr and let javascriptdifference in var letvar vas letdifference between variable created using let 2c var and consjavascript let var differencesvar vs for javascriptlet in javascript vs vardifference between let and var in jsshould i use var or letwhat is teh difernece bwetween var and letbetween var 2c let 2cts differende between 5bvar 5d and var 5b 5dcvariable vs let in jswhat 27s the difference between let and var in javascriptfdiffernce between var and letvar vsvar vs letnodejs var vs letjavascript var or letlet vs var in javascriptdiffernece between let and varvar and let difference in jswhat is the difference between let cont and varwhat the difference between var and letusing var instead of letdifference beetwen var and let in jsdifference bw var letwhen to use let vs vardifference wetween let and vardiffrent between var and letdifference between var an letlet and var difference in jsdifference between var and letlet and var difference with examplelet and var difference exampledifference var and let javascriptjavascript the meaning of var and letvar vs let javascriptdifference in let and vardiff btw var and letwhat is the difference between let 2cvar and const in javascript4 what is the difference between var and letwhen to use let or vardifference let and varvar vs let in node js what is the difference between let and var 3fnode let vs vardifferent between var and letdifference in let and var in javascriptjavascript what is the difference between var and letlet versus var in javascriptmajor difff between let andd var consstdifference between let 2c const and var in javascriptdifference var letwhat is difference between let and var and const in javascriptdifferent between let and varvar e let differencejavascritp use let or vardifference between let 2cvar and constjavascript var and let differencewhat is the difference btw let and varwhat is the difference between var and let keywordlet var javascriptjavascript let var differencelet vs var differencewhat is difference between let and var in javascriptdifference between let and var jddiff bet let and vardifference betweeen let and vardifference between var and let in for looplet var differencediffrence between var and let in js 27what are the differences between variables created using let 2c var 2c and const 3fla difference const var letwhere and let in javascriptdeiffrence between let and varlet var difference for loopvariable and let differencewhat is the major difference between let and var in javascriptlet and var in jsdifference between var let and var in javascript with examplewhat is the difference btw let and var 3fdiff b let and var jswhat are the differences between variables created using let 2c var or const 3fdifference between let and var 5cdifference btwn var and letvar vs ketnodejs let vs vardifferences between let and varjs var scope vs letjavascript let and varvar vs let vsjavascript var vs let vsshould i use let or varwhats the difference between let and varnode js difference between let and varvar let differencedifference between var const 26 let in javascriptvar and let differencedifference between var const and letrdifference between let and var jsjs differnce between let and vardoes changing var to let make a big difference in javascriptwhat are the differences between variables created using let 2c var 2c and const 3f 2avar difference letdifference between let and var in javascript w3schoolswhat is the deffrence in var and let in javascriptjavascript var let differencejavascript can use var ot letjs var vs letvar vs let examples jswhat is the difference between declaring a variable with let and without letwhat 27s the difference between let and vardiffernece between var and let jsvar and let differencesvar or letdiif between var and letlet var const difference javascriptwhat is the diffence between the let and var in javascpritdifference between let and var keyword in javascriptdiff let and varlet vs varhow is let different than var javascriptuse of let and var in javascriptwhat is deffrent between var and letfunctions with var vs letdifference between var and let in javascriptwhen to use var and let in javascriptlet or var in javascriptdifference between let and var difference between let var and costdiff between var and letwhat is the difference between var and let and why is let preferred 3fdifference of let and varwhat is difference between var and letvar and let in jsdiff b 2fw let and var in jsvar vs letdifference between javascript var and letjavascript 2b difference between let and varlet or var javascriptjs whats the difference between let 2c var 2c and no keywordvar or let 3fdifference between var let costdifference let and var javascriptdifferneces bewteen the var and let keyword differance between var and letdifference between var and let and constjs difference between var and letdifferentiate between let and vardifference between let vs varjavqascript var vs let let vs var in functionjavascript let or vardifferent between using let vs varvar vs let javascirpvar vs let in javascriptdifference var vs letthe difference between var and letwhat is the differnce between var and let in javascriptdifferenec between var and let jswhat is the difference between var and letlet function or var function javascriptwhat 27s the difference between var and let in javascriptdiff let vs varvar vs let jsdiffernce between var and letjs different between let and vardifferences between var and letlet var difference typescriptjs var vs letydifference of var and letlet and var differencewhat is difference let and vardifference var and let in javascriptmdn let vs varvar versus letjavascript var vs letlet and vardifference beteen let and varwhat is difference between var and let in jsdiffernece between var and letwhat are the difrences between var and letvar vs let keywordlet var as type 3d 2a what are the differences between variables created using let 2c var or const 3fjavascript let var differnecevar or let in jswhat are diffrents between let and var 3fjs use var or letdifference between let put and varjavascript should you use let or varwhat is the difference between var and let 3fjavascript let vs vardiff var and letdifference b 2fw let and var in jswht is difference between let 2c const and varlet vs var javascriptthe difference between var and let in javascriptwhat is the difference between var let and const in javascriptdifference b 2fw let and vardifference var and letjquery var or letwhat is the difference between const var and let in javascriptdifference between var and let in javascript in hindilet and var difference