showing results for - "difference between let and var in javascript"
Allyson
28 Jan 2020
1The let statement declares a block scope local variable, optionally initializing it to a value.
2
3let x = 1;
4
5if (x === 1) {
6  let x = 2;
7
8  console.log(x);
9  // expected output: 2
10}
11
12console.log(x);
13// expected output: 1
Amine
07 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
Francisco
28 Aug 2019
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();
Nelia
22 Feb 2016
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}
Sebastián
12 Sep 2016
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
queries leading to this page
let var javascriotlet const var in jslet var differencewhat is let variable in javascriptvar vs let exampledifferance between var and letlet vs var w3schoolswhat does let do in javascriptdiffernce between var and letdifference between let and var jdwhat is difference between let and var and const in javascriptjavascript release letlet function jslet javscriptlet vs var jsvar vs for javascriptlet javascript exampledefine javascript letjavascript using letwhat is let jsimplement let in javascriptvar and let in javascriptwhat 27s the difference between using let and var to declare a variable in es6 3fdifference between const 2c let and var in jslet i 0 i javascriptcan you define a key of 60let or var 60 in any objectjavascript var vs letjavascript difference between let and varvar let and const variable w3schoolslet in which javasript scriptdifference between let and var and const in javascriptlet variable scope javascriptdiffrence between let and varw3 let constlet var javascriptwhen to use let vs varwhat is the difference between let or vardifference of var and lettype let javascriptwhat does let do in javascript 3fvar vs let javascriptlet in jasnode js letwaht is difference between var and letdifference between let and var javascriptdifference between var let and javascriptin javascriptwhat are diffrents between let and var 3flet dunction result javascriptwhy we need let in javascript what 27s the difference between 22var 22 vs 22let 22 in javascript 3fjavascript let 3d functionjavascript let and variableslet vs var examplelet has functional scopewhat does let stands for javascriptdifference bewteen let and varjavascript let infor let javascriptjavascript different between var and letwhat is difference between let and var in javascriptlet and var differencejavascript the meaning of var and letdifference between let and var and const in javascript w3schoolswhat does let mean in javascriptwhat does let means in javascriptlet and var in jslet statement javascriptjavascript declare variable with letlet variable meaning in jsdifference b 2fw let and varjs let 23js difference between let and vardifference let and var javascriptjavascript var and letvar and let javascriptlet and var difference in javascriptlet java scriptlet 5bvariable 5d jswhich is better let or vardifference let and varuse let or var in javascriptlet and var difference examplelet vs const in jsonjs let meanswhen to use var vs letwhats the the difrence between let and varjs difference let and varlet in javascript meaningvar vs let in javascriptlet keyword in javascriptjavascript let vs var differencewhen use let in jslet in jsvar let const jslet of statement javascriptvar vs letlet in js meansis let in javascriipt a vardifference between let and var 27diff bet let and varvlet js js let and var exercise projectdifference between var and let keywordslet constjavascript difference var letlet meaning in javascriptdifference between var and let in javasxriptshould i use let or vardiffernece between let and varlet variable javascriptjavascript var vs let vsjavascript for letw3schools js letjava sript let functionlet keyword javascriptlet i jshtml let or varif let and var keyworddifference between let and var let in loop javascriptvar let jsdifference bw var letdifferences between var and letlet i 3d 0 javascriptvar vs let in node jslet characters javascriptjavascript letinwhat is the difference between let and var 3fwhat is the difference between changing and reassigning ecma in javascript the main differences between let and var variable declarations aredifference between var and let in javascriptwhat does let mean in jsdiffernece between var and letwhat to use let vs var jslet node jsdiff b 2fw let and var in jsjs let meaninglet in javascriptconst and let jsvar vs letlet jsdifferentiate between let and varwhat is different between let and vardifference between let and varwhat is let used for in javascriptlet and var example in javascriptlet versus var in javascriptwhat is the meaning of let in javascriptwhat means let in javascriptdifference between var ans letdifference between let const and var javascriptcan you do if and let javascriptdiffrence between let and var javascriptlet 7b 7d in javascripthow to allow 24 7bvar 7d in javascriptwhat is the difference between var and let in javascriptdifference between let or var in javascriptlet 7b 7d javascriptlet of jswhen do we use let in javascriptmeaning of let in jsis it better to use let or varwhat is let javascrioptintroduction to es6 let keyword in w3schoolsjavascript var vs let example in for loopvar javascript 22 vs 27what is the difference between var and let 3flet variable jsdifference var and let javascriptnode js var vs letwhat is the difference between variables created using let 2c var 2c and const 3fconst and let in javascriptjavascript let var differencejavascript let keywordwhat is the difference between var and let in js 3f2 what is the difference between let and var 3fwhere is let used in javascriptlet use in javascriptlet a meaning jslet or javascriptwhat is the diffence between the let and var in javascpritwhat is let in javascriptfunction expression without let or constjs let of let inlet 3a javascriptthe difference between var and letlet in react jsvariables let vs varlet of js let inlet of and let in in jslet of function jslet w3js var or letwhy do we use let in javascriptjavascript for let injavascript let const factorsvariables declared with the let keyword have this kind of scope var let const w3const let var in javascript w3schoolsconst let and var javascripthttps 2f 2fjavascript the lettdiffrence between var and let let in javascriptdifference in var letwhat should i use let or varwhat are the differences between variables created using let 2c var or const 3flet and varjavascript release data letdifference between avr and let javascriptdiffference between let and var in jsjavascript should you use let or varwhat is difference between var and let in javascriptdifference between var const and letjavascript difference let varwhat does let stand for javascriptconvert let to var scopediffernce in let var jswhats better to use in javascript var or letvar let in javascriptvar vs let keyworddifference between var and let in jsdifference between var const and let in javascriptlet en javascriptwhat is t 3dlet in jswhat is the difference between let and varscope for letlet string javascriptlet vs var in javascript programizdifference between let const and var in javascriptlet js meaningvar javascript vs letvar and letlet vas varnodejs let functionlocal variable javascript var or letvar let javascriptlet in javascriotdifference between let cont and varlet x 3dy 3d0 in javascriptblock scope htmljavascript letvar or let javascriptwhat is use of let in javascriptdiffernce between let and varlet a b 10 javascriptdifference between var and let in javascript in hindilet type in javascriptlet const var javascriptlet vs var javascriptis var and let the same thingjavascript var let scopejs lef in numberdifference between let and var keywordvar let differencelet in javascro 5btdifference between let and var jswhen to use var and let in javascriptlet is jslet a 3d 2 javascriptlet x 3d newlet function in javascriptvar letvar let andleth javascriptajvascript letlet in javascript vs varwhat 27s the difference between var and let in javascriptdifference in let and var in javascriptw3schools let javascript 2blet javascriptdifference between let var and costlet and var difference with examplewhat is the difference between var and letlet that 3d this javascrtiptlet that 3d this javascriptlet in javascript definitionjscript for let inwhy use let in javascriptdifference bwtween var and letjs var vs letyfor let in javascriptjavascript let scopewhat is the diffrence between var and let jslet function scopedjavascript let forjavascript use var or letw3schools let let vs varrvar vs letlet and cont in javascripthow to use to let in javascriptonline javascript text let and varwhat is the difference between var let and const in javascriptlet vs var vs 3dwhat is let and var in javascriptjavscript let vs varwhen to use var or let in jslet jquery 3d 24why don 27t we use let in javascript let in jslet 3d 7b 7d as jsvar or let in jswhag is let in jsthe difference between var and let in jsdifference between const var and let javascriptdifference between let an d var in jswhen to use var and when to use letdifference between var and let jswhat let means in javascriptlet vsr in for loop javacriptsjavscript letwhat let in jsjavascript let or varfdiffernce between var and letwhat is the differnce between var and let in javascriptlet javascript w3schoolslet javascript definitionlet jquerylet in jsonlet currentstep 3d 1 javascriptlet javascript esjavascript 2b difference between let and vardifference b 2fw let and var in jsdifference between cont var and letjavascript let functiondiffrence between var and let in js 27let javascript vs varlet in javascript scopewhen to use let or varjs let full namelet var const difference javascriptdeiffrence between let and varletin javascriptdeclare function javascript letwhat is the difference between let 2cvar and const in javascripthow does let difference from var in javascriptjava script var vs letfor 28let javascriptdiffrenece between var and let in javascriptlet vs var in jslet x in javascriptlet const var in javascriptlet 28lexical 29 and const 28unrebindable 29 bindingswhy is a variable called let in javascriptwhat is teh difernece bwetween var and letvariable let keywordjavascript what is the difference between var and letlet vs var in javascriptlet in javaascriptlet 28 29 jsdefrent between let and var in jsuse of let in javascriptlet type javascriptwhat is let in nodejsjavascritp use let or var 2a what are the differences between variables created using let 2c var or const 3fdifference between var let and vardiff let and varjs let and varlet javascriptcreate a let list javascriptdifference betweeen var and letdifference between let 2c cont and varwhen do you have to use let in javascriptmdn let vs varvar and let jslet keyword jqueryfunctions with var vs letcss for let i in xuse of let and var in javascriptlet data type in javascriptin javascript let standsin javascript 2c variables declared with the let or const keywords have block scope let in node jsvar and let differencelet var jsjavascript use let or vardeclare variable let javascriptvar vs let in jsjs difference let vardifference between let an varlet or var javascripthow to set let in javajshow to let in javascriptvar and let difference in jsdifference var letlet and const in javascriptlet types in jsis let or var better in jswhat is diffrence between let and var in jsjs letdifference between using 22let 22 and 22var 22 3fwhat is let keyword in javascript 3fjs difference of let and varnodejs let const let var in javascriptjavascript can use var ot letwhat is var and let in javascriptis let in js used let w3 jsdeclare let in javascriptlet 28 29 javascriptfor and let in javascriptjavascript let var differnecewhen do you use the let keywordlet i of s javascriptlet meaning in jswhat are the differences between variables created using let 2c var 2c and const 3f 2ajavascript difference between var and let 3c 25 letlet variable function javascriptvar or let 3fjavascript let vs varlet javascrfi 5bptjs what is letdifferences between var and let javascriptjs let keywordwhat 22let 22 means in javascriptjs what means letlet vs var javascriptjs let functionlet and var diff in javascriptlet body jswhat is the difference between var and let 3f 2ajs var letdifference in let and varjavascript different let and var2 what are the major differences between let and var in javascript 3fwhat is the difference between const var and let in javascriptwhat is difference between var and let in jslet javavar vs 24differences between let and varwhat should we use 3flet or varwhat is the difference between e2 80 9cvar e2 80 9d and e2 80 9clet e2 80 9d in javascript 3fjs what does let dojavascript let and var differencejs var vs letwhat is the deffrence in var and let in javascriptvar const letwhat 27s the difference between let and var in javascriptscope of let in javascriptlet i javascriptwhy cant we define a variable as let in global scopevar vs let jslet in javascriptjavascript let 3d 7b 7ddiff between var and letvar let and const in javascriptlet in jsfor loop let jslet and var in javascriptjavascript variable letwhat is the difference btw let and vardifference b 2fw var and letdifference betweeen let and varlet javascripcvariable vs let in jswhat doese let do in javascriptlet scope javascriptjs difference between var and letdiff between let and var in javascriptjavascript let of vs let indifference between 22let 22 and 22var 22 3fvar vs let examples jslet keyword in javascript 3fdifference between var let and var in javascript with examplenode js var and letlet var 3d 5b 5d in javascriptdifference betweem var and letwaht does js letlet and var jssyntax let in jslet var difference javascriptnode let vs varlet i 3b javascriptwhat is lett in javascriptlet function 3d jsvar and let differencesdifference between let vs vardifference in var and letlet keyword in javalet and var difference jsjavascript why use let instead of vardiffrent between var and letywhere to use var and letlet statement in javascriptlet vs varlet vs var in functionwhat is dif between var and letjavascripts letvar let difference jslet variable declarationdifference var and letdifferent between let and varwhat is let in jslet and var difference javascriptwhat are the differences between variables created using let 2c var 2c and const 3flet const in javascriptdifference let varint vs let javascriptget let from function jswhat is a let in jslet in javascriptmeaning of let in javascriptlet es6 javascriptwhen should use let in javascriptlet i 3b in javascriptjs let inwhat 27s the difference between let and varshould you use let in javascriptlet use in jshow to use let in javascriptwhat is let javascriptlet and constjavascript should i use var or letwhen should we use let in function in javascriptlet instead of var javascriptdifference between var and let javascriptvar and let difference in javascriptlet 7b 7d 3d javascriptwhat does let 3d 7b 7d mean in jswhat does let do javascriptlet or var in javascriptjavascript block scopelet vs varjavascript variable let vs varjs whats the difference between let 2c var 2c and no keywordlet this javascriptdiff bt let and varhow to use let in jaba scirptdiff c3 a9rence var et let javascriptdifference between let and normal variable in jsare let and var samejavascript let var differenceslet in csslet var difference for loopjs let varwhat is let in javascriptconvert var to letjavascript var and let differencelet var difference jslet htmlfor let w3schoolslet of javascriptshould i use var or letvar let const w3schoolsdifference between let and var in javascript with examplelet scope in javascripthow to make the default as let in javascriptvar scope vs let scopewhat does let do in jslet in javascrept what does it menswhen use var or letwhat is the major difference between let and var in javascriptdifference between let var in javascriptlet vs var w3javascript add value to letlet variableshould i use let or var in javascriptdelcaring a let variable in javascriptlet javascript vad g c3 b6r denwhy use var instead of let in jdiff between let and varnode js let vs vardeclaring let versus var jslet 5b 5d javascriptconst let jsfor 28let in 29 jsdifference beteween var and letdiff btw var and letwhat does let do in java scriptvar vs let javascirp let javascriptdiff let vs varlet and var difference in jswhat is let n javascripthow to use let variable outside function in javascriptjavascript let variablereact js letthe difference between var and let in javascriptdeclare variable javascript letjs block scope variablelet vs var javascript exampledifferent between var and letjavascript var let differencedifference between var const 26 let in javascriptvar or let in javascriptlet keyword in jshtml let vs vardiff b let and var jsdifference between var and let javascfiptvar vs ketlet and var keyword differencedifference between let and var in jslet function javascriptjavascript let infor let of function javascriptnodejs var and letusing let in javascriptdifference var and let in javascriptlet javascriptlet and const jsdifference between var and let n jsvar vs let in node js filewhat is let in node jslet js w3what let in javascriptlet i in javascriptlet javascript meaningjavascript let meaningjs let vs varthe difference between let and varthe difference between let and var in javascriptdifference var et letdifference between let and var in javascriptfor let in jswhat is the difference between let and var in javascriptvar vs let w3 schoolsdifference between let and var keyword in javascriptjs let and constwhy do you use let in java scriptjs let or varlet key wordw3schools var let what is the difference between let and var 3fwhat is let keyword in javascriptdifference between let and var in javascript w3schoolsdifference between var and letlet and var javascriptlet function in jsthe difference between a var and a letlet scope jsdifference between let 2c const and var in javascriptlet var const javascript examplelet var function javascriptnodejs var vs letlet javasciptdifference between let and var in javascript