showing results for - "difference between let and var"
Sara
09 Apr 2020
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
Hamza
05 Jan 2021
1In simple words 'var' is function scoped and 'let' is block scoped
Luca
02 Jan 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();
Adrián
01 Jan 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}
Maximilian
13 May 2017
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.
Bruno
03 Jun 2018
1Scoping rules
2
3
4/*The main difference is scoping rules. 
5Variables declared by var keyword are scoped to the 
6immediate function body (hence the function scope) 
7while let variables are scoped to the immediate 
8enclosing block denoted by { } (hence the block scope).**/ 
9
10function run() {
11  var foo = "Foo";      //Function scope
12  let bar = "Bar";
13
14  console.log(foo, bar); // Foo Bar
15
16  {
17    var moo = "Mooo"
18    let baz = "Bazz";                   //Block scope
19    console.log(moo, baz); // Mooo Bazz
20  }
21
22  console.log(moo); // Mooo
23  console.log(baz); // ReferenceError
24}
25
26run();
27
28// Check out the source link below from stack overflow for more insights
queries leading to this page
javascript let var differencelet var differencevar let difference jsdifferance between var and letwhat is the difference between var and let in js 3f2 what is the difference between let and var 3fdiffernce between var and letdifference between let and var jdwhat is difference between let and var and const in javascriptdifference var and letwhat is the diffence between the let and var in javascpritlet vs var jsvar vs for javascriptdifferent between let and varlet vs var differencejavascript 2b difference between let and varwhat are the differences between variables created using let 2c var 2c and const 3fdifference b 2fw let and var in jsthe difference between var and letdifference between cont var and letdiffrence between var and let in js 27difference let varvariables let vs varlet var const difference javascriptdeiffrence between let and varwhat 27s the difference between using let and var to declare a variable in es6 3fdifference between const 2c let and var in jsletin javascriptwhat is the difference between let 2cvar and const in javascripthow does let difference from var in javascriptdifference between 22let 22 and 22var 22 3f 27javascript difference between let and varjavascript var vs letjava script var vs letdifference between let and var and const in javascriptdiffrenece between var and let in javascriptdiffrence between let and varlet vs var in jsdifference between 22let 22 and 22var 22 3f 27 27what 27s the difference between let and varwhen to use let vs varwhat is teh difernece bwetween var and letvar vs let es 3fjavascript what is the difference between var and letlet vs var in javascriptdiffrence between var and letwhat is the difference between let or vardefrent between let and var in jsdifference of var and letvar vs let javascriptdifference in var letdifference between var and let javascriptwaht is difference between var and letdifference between let and var javascriptvar vs let declarationdifference between var let and javascriptin javascriptvar and let difference in javascriptwhat are diffrents between let and var 3flet vs varwhat are the differences between variables created using let 2c var or const 3flet and varlet vs var vaibalewhat 27s the difference between 22var 22 vs 22let 22 in javascript 3fjs whats the difference between let 2c var 2c and no keyworddiffference between let and var in js 2a what are the differences between variables created using let 2c var or const 3fdifference between avr and let javascriptdifference between var let and varwhat is difference between var and let in javascriptdiff bt let and vardiff let and vardifference between var const and letlet vs var examplejavascript difference let vardiff c3 a9rence var et let javascriptdifference between let and normal variable in jsare let and var samejavascript let var differencesdifference bewteen let and varlet var difference for loopdifference betweeen var and letdifference between let 2c cont and varjavascript var and let differencediffernce in let var jsjavascript different between var and letlet var difference jsdifference between var const and let in javascriptvar vs let keywordwhat is difference between let and var in javascriptlet and var differencewhat is the difference between let and varjavascript the meaning of var and letvar and let differencelet and var in jsvar vs let in jsjs difference let vardifference between let an varlet or var javascriptdifference between let const and var in javascriptvar javascript vs letwhat is the differnce between var and let in javascriptdifference between let var in javascriptvar and letdifference b 2fw let and varvar and let difference in jsjs difference between let and vardifference var letdifference let and var javascriptlet and var difference in javascriptdifference between let cont and vardifference between using 22let 22 and 22var 22 3fdiference between let and varjs difference of let and vardiff between let and varjavascript can use var ot letdifference let and vardiff btw var and letdifference beteween var and letdiffernce between let and vardifference between var and let in javascript in hindilet and var difference examplelet vs var javascriptis var and let the same thingjavascript let var differnecewhen to use var vs letwhats the the difrence between let and varwhat are the differences between variables created using let 2c var 2c and const 3f 2ajavascript difference between var and letdifference between let and var keyworddiff let vs varvar let differencejs difference let and varvar or let 3fvar vs let in javascriptlet and var difference in jsjavascript let vs varjavascript let vs var differencedifferences between var and let javascriptthe difference between var and let in javascriptvar vs letlet vs var javascript exampledifferent between var and letlet vs var javascriptjavascript var let differencedifference between var const 26 let in javascriptdifference between let and var 27diff bet let and varlet and var diff in javascriptdifference between var and let keywordswhat is the difference between var and let 3f 2ajavascript difference var letdifferences between let and var 3flet in javascript vs varwhat 27s the difference between var and let in javascriptdiff b let and var jsdifference in let and vardifference between var and let in javasxriptdifference between var and let javascfiptjavascript different let and var2 what are the major differences between let and var in javascript 3fvar vs ketwhat is the difference between const var and let in javascriptdiffernece between let and varlet and var keyword differencewhat is difference between var and let in jsdifference in let and var in javascriptvar js vs letjavascript var vs let vsvar vs 24differences between let and varlet or vardifference between let and var in jsvar vs letdifference between let var and costwhat is the difference between e2 80 9cvar e2 80 9d and e2 80 9clet e2 80 9d in javascript 3fjavascript let and var differencedifference between 22let 22 and 22var 22 3f 27 27 27js var vs letlet and var difference with examplevar vsvar vs letdifference var and let in javascriptif let and var keyworddifference between let and var what is the difference between var and letdifference bw var letwhat 27s the difference between let and var in javascriptdifferences between var and letdifference between var and let n jsdifference bwtween var and letjs var vs letylet vs var vsvar vs let jswhat is the difference between let and var 3fdiff between var and letwhat is the diffrence between var and let jsjavascript use var or letin javascript the main differences between let and var variable declarations arejs let vs varlet vs varrdifference between var and let in javascriptthe difference between let and varwhat is the difference btw let and vardiffernece between var and letthe difference between let and var in javascriptdifference var et letwhat to use let vs var jswhat is the difference between var let and const in javascriptdifference b 2fw var and letdiff b 2fw let and var in jslet vs var vs 3ddifference betweeen let and vardifference between let and var in javascriptjs difference between var and letwhat is the difference between let and var in javascriptdiff between let and var in javascriptdifference between 22let 22 and 22var 22 3flet vs var in fordifference between var let and var in javascript with exampledifference between let and var keyword in javascriptdifferentiate between let and vardifference betweem var and letwhat is different between let and vardifference between let and varlet var difference javascriptnode let vs vardiference between var and letdifference between var ans let what is the difference between let and var 3fdifference between let const and var javascriptdifference between let and var in javascript w3schoolsvar and let differencesthe difference between var and let in jsdifference between let vs varwhat is difference between var and letdifference in var and letwhat is the difference between var and let in javascriptdifference between let or var in javascriptlet and var difference jsdifference between var and letdifference between const var and let javascriptdifference between let an d var in jsthe difference between a var and a letdiffrent between var and letydifference between var and let jsdifference bw var and letwhat is difference between var and let 23 3fvar javascript 22 vs 27difference between let 2c const and var in javascriptwhat is the difference between var and let 3fjs difference var and letlet vs varfdiffernce between var and letdifference var and let javascriptwhat is the difference between variables created using let 2c var 2c and const 3flet vs var in functionwhat is dif between var and letdifference between let and var