difference between var and let and const in javascript

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

showing results for - "difference between var and let and const in javascript"
Lyndon
27 Aug 2018
1var: 
2	- hoisted (always declared at top of scope, global if none)
3    - function scope
4let:
5    - block scope
6    - not redeclarable
7const: 
8    - block scope
9    - not reassignable
10    - not redeclarable
11    
12Note: Although it may seem like these hold only semantic meaning, using the
13appropriate keywords helps the JS engines' compiler to decide on what to optimize.
Neil
08 Nov 2020
1//let is used to declare the variable or initialize a variable..
2//let variable can be updated in future.
3//Its block scope and we cannot redeclare it.
4
5let a; // Initializing the variable.
6let b = 4; // Initializing and declaring variable.
7let b = 5; //Error:- cannot redeclare a value but we can update the value.
8
9//var is just like let but var is used to declare a variable value at top of
10//the program, its hoisted.
11
12var a; // Initializing the variable.
13var b = 4; // Initializing and declaring variable.
14var b = 5; //b will be redeclared and we can update its value.
15
16//const variable should be declared and intialized at same time.
17//const variable cannot be updated in future.
18//we cannot even redeclare the variable in future.
19
20const a = 10; //Initilizing and declaring a variable at a time.
21const b; //Error:- const should be declared and intialized.
22const a = 11 // Error:- cannot redeclare the variable again.
Lex
11 May 2018
1The difference is that with const you can only only assign a value to a variable
2once, but with let it allows you to reassign after it has been assigned.
Art
06 Oct 2019
1// ⚡ Initialization
2var a; //✅
3let b; //✅
4const c; //❌ const must have an initial value
5
6// ⚡ Re-Assignment
7var d = 1; 
8d = 10; //✅
9
10let e = 2; 
11e = 20; //✅
12
13const f = 3; 
14f = 30 //❌ const value is always constant and cannot be reassigned
15
16// ⚡ Re-Declaration
17var g = 1; 
18var g = 10; //✅
19
20let h = 2; 
21let h = 20; //❌ must use variable name only to reassign, cannot be reassigned 
22
23const i = 3; 
24const i = 30 //❌ cannot be reassigned
25
26// ⚡ Scope [Global, Function or local and block scope]
27// Global Scope ==> Any variable that is declared outside any of the function
28// Function Scope(var) ==> Can be accessed inside the function it was declared and its child
29// Block Scope(let and const) ==> Con be accessed inside curly brackets where it was declared and its child
30
31function myFunction(){
32    var myName1 = 'Asmita';
33    let myName2 = 'Dikshit';
34    const myName3 = 'Sizen';
35    console.log(myName1);
36    console.log(myName2);
37    console.log(myName3);
38
39    if(true){
40        console.log(myName1);
41        console.log(myName2);
42        console.log(myName3);
43
44        var myAge1 = 13;
45        let myAge2 = 15;
46        const myAge3 = 14;
47        console.log(myAge1);
48        console.log(myAge2);
49        console.log(myAge3);
50    }
51
52    console.log(myName1);
53    console.log(myName2);
54    console.log(myName3);
55
56    console.log(myAge1);
57    console.log(myAge2);
58    console.log(myAge3);
59};
60myFunction();
Dante
17 Oct 2016
1//functional scope 
2 var a; // declaration
3 a=10; // initialization; 
4//global scope
5// re-initialization possible
6 let a;//only blocked scope & re-initialization possible
7 a=10;
8let a =20;
9if(true){
10  let b =30;
11}
12console.log(b); // b is not defined
13const // const also blocked scope,Re-initialization and re-declaration not possible
14const a; // throws error {when we declaring the value we should assign the value.
15const a =20;
16if(true){
17  const b =30;
18}
19console.log(b); // b is not defined
20console.log(a); // no output here because code execution break at leve b.
21
Julián
03 Oct 2020
1var variable1 // declared using var
2const variable2 // declared using const
3
4myFunction1();
5
6function myFunction1() {
7  variable1 = "hello!";
8  console.log(variable1);
9  // "hello!"
10}
11
12myFunction2();
13
14function myFunction2() {
15  variable2 = "hello!";
16  // error: variable2 is a constant and can not be redifined
17}
18
19myFunction3(true);
20
21myFunction3(codition) {
22  if(condition) {
23    let variable3 = "helo!" // declared using const
24  }
25  variable3 = "hello!";
26  // error: variable3 is out of scope
27}
28
29/*
30
31Therefore, 
32  
33  var:
34    -can be declared outside any function to be used inside any function
35    -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
36    -can be changed again and again anywhere
37  let:
38    -can be declared outside any function to be used inside any function
39    -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
40    - can be changed again and again only inside the statement in which they are made in
41    const:
42    -can be declared outside any function to be used inside any function
43    -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
44    -cannot be changed again and agan anywhere, if tried to, will result in an error
45
46*/
queries leading to this page
let vas const vs vardiffrence between let const vardifference in const and letconst vs let in javascriptwrite the difference between var let and const with code examplesjavascript let vs var vs constdifference between let const and varwhats the difference between let and const 3fwhat is the differences between js var 2c let and constjavascript differenct let var constwhat is the difference between 22let 22 and 22const 22 in javascriptdifference between let and var and const in javascript w3schoolswhat isthe difference bet ween let and var and const in javascriptjavascript let vs const vs varjavascript difference between var let and constjs let var const diffdifference between const let and var keyword in javascriptdiff between var let and constvar vs let vs constlet vs var vs const typescriptconst vs var vs letconst vs var vs let in javascriptis there an actual difference between var 2c const 2c and letdifference between let var const in jsdifferent let en constwhat is the difference between let var and const in javascriptdiffernece between var let constwhat is the difference between let and const 3f what distinguishes both from var 3fjs let vs constvar vs let vs const in jsfdifference between let and const in jsthe difference between const and letlet vs var vs const jswhat are the differences between variables created using let 2c var 2c and constlet var and const differencedifferences between var let and constdifference between let an constthe difference between const and var in javascriptdifference between let and constant in javascriptwhat is the difference between const and let in javascriptvar vs let vs const jsjavascript difference between let and constwhat is difference between const vs let vs varwhat is the difference between var let and const 3fdifference between let and const jsdifference between var let const in jsdifference between let const and var in javascriptjavascript difference between const and vardiffrence between var 2c let constjavascript constant vs letvar let vs constconst vs let in jswhat is the difference between let 2c var 2c and const 3fconst and let differencediffence between const let var in jsjs difference between let and var and constdifference between var 2c const 2c letlet var and constwhat is the different between var and let and const in jsconst vs let globaljavascript let var and constwhat is the difference between var let and constdifference let const varlet vs const referencewhat is difference between const let and var typeswhat is the difference between const let and vardifferent between let const and varwhat the differences among var 2c const and let 3fdifference between const and varcan you use let and const with the same variable in jsdifference of var let and constdifference between let 2cvar and const in type scriptwhat is the difference betwen var and let and const 3fconst javascript vs letjavascript difference let constlet vs var vs const node jsdifference between const and let in nodejsdifference between var let and const in javascript with examplejs const vs let vs varwhat is var let and const in javascriptconst vs letdiffrence between const and var in js 5cdifferece between var 2cconst 2cletwhat diferrence in let 2c var and const in javascriptdiff bw var let and const in jsdifference with const let in functionsdifference between var let and const in typescriptdifference between var let constlet const differencewhat difference in let var and const in javascript with exampledifference between var and let and const with exampledifference between const and let in jsdifferences between var let and const javascriptwhen to use var let and const in javascriptjavascript use var or let or constjavascript var vs const and letvar vs let vs const exampledifference between var and const in jsjavascript let vs constdifference between var 2c let and const in javascriptwhat is the difference between let and const in jsconst vs var vs let javascriptwhat is difference between let var and const in javascriptdifference between let and const in es6different between let and const and vardifference between var 2clet and conststatic vs const vs let vs varlet and const difference in javascriptdifference between let and const in javascriptdifference between let var and const in javascriptvar vs let vs const in javascriptdifference between let and const in javascript with examplenode js let vs var vs constwhy is var let and const called in javascriptjavascript const vs let vs varconst vs let vs var in jsconst vs let vs var in javascriptdifference between variable var and let and const in javascript with exampleis there a difference between let and const variable in js 3f what is the difference between const and var in javascriptdifference between let var const javascriptdifference between let and const var in javascriptdifference between let var and const in jswhat is difference between var let and const in javascriptcompare between let and var and const in javascriptdefference between let const and var in jsvar and let and const differencedifference between var and const in javascriptdifferences between var 2c let and constlet vs const vs var in javascriptdifference between const and var and letdiff b 2fw var let const jswhat is the difference between var 2c let 2c and const diff between var let constconst vs let vs anydiff let vs var vs constconst vs val vs letdifference among var 2clet 2cconstdifference between const var and letlet vs var vs const in jswhen to use let vs constdifference between between let and constdifference between const and let and var in javascriptwhat is the difference with const var letlet and var and const differencelet vs var vs const in javascriptlet vs const vs var faster to callwhat is the difference between const var and let in javascriptlet and const vs vardifference const let javascriptwhat is the difference between var let and const latest newsconst vs let vs varwhat is the difference between let 2cvar and const in javascriptwhat is the difference between const var and letjavascript difference between let var constdiffernce between var let constlet const and var in javascriptdiffrence in let var const what are the differences between var 2c let and constdifference var let constdifference betwen let var and constlet vs const vs var in jsdifferenec between const and var and let javascriptwhat are the differences between var let and constdiff between let and const and vardifference between var 2c let and constwhat is the difference between var let constjavascript const vs var vs letdiff between let and const constjavascript difference const et letjs const vs letlet vas const vs var exampledifference between const 2c var and let in jsdifferent between var let constwhat is difference between let const varwhat is the difference between declaring variables using var 2c let and const let vs var vs const javascriptdifferenc between var let and constvar vs const vs letwhat is difference between var and let and const in javascript with examplewht is difference between let 2c const and vardifference between let 2c const and var javascriptlet 2c const and var difference between variablejs const vs var vs letdifference between var and let and constdiff between let and const in javascripttypescript const vs let vs varwhats the difference between var and const in javascriptvar vs let vs const javascriptwhat is the difference between let and const in typescriptwhat is the difference between let and const in javascriptwhats difference between let const and vardifference between var 2c let 2c and const keywords in javascriptdifference between const var and let javascriptjavascropt let vs var vs constdifferences between let const and vardifference between var const let in javascriptvar let const javascript differencedifference between const 2clet and vardifference between var let constwhat are the differences between var 2c let and const 3fdifference between let const and var using for loopdiffernce between let const vardifference between var const and let in javascriptjavascript let const differencevar let const difference jslet vs const vs var vs freezewhat is difference between let var and constwhat is difference between var const letconst var let differencedifference between let 2c const and var jsvar let and const in javascriptlet vs const vs var in nodejswhat is the difference between let and var and const in javascriptdifference between var const 26 let in javascriptlet vs var vs const speeddifference of let and constvar let and const differencelet vs const vs vardefferent between const let varvar vs const vs let jsconst and var vs lettypescript var vs let vs constconst let var differencedifference between let and const and var what is the difference between const 2c let and varvar vs condt vs letjavascript when to use let vs constjs let vs var vs constwhat is let and const in javascriptdifference b 2fw var let and constwhat is different between let and constwhat is the diffrence between const 2clet and var in javascriptdifferences between declaring variables using var 2c let and constjavascript for let vs constdifference between let const varlet and const diff jsdifference between const var and let in javascriptjs var vs const vs letdifference between const and let and vardiffrence between let and constexplain the difference between var 2c let 2c 26 constlet const vs vars in javascriptjs variables differences var let constdiffernce between var 2clet 2cconstwhat is difference between var let constjavascritp let vs constdifference between var and let and const in javascriptdifference between var 2c let and const in javascript and best use caseswhat is the difference between const and let in nodewhat are the differences between let and constjavascript var vs const vs letdifference var let const javascriptvar vs let vs const javascript with exampleslet and const variables in javascriptwhat is difference between let and var and const in javascriptvar vs const y letdiffernce between var and let and const jsvar 2c let 2c and const e2 80 93 what 27s the difference 3fwhat is the difference between const and let 3flet var const differencelet vs const jsdifferance between var let constdifference between var and const javascriptlet and const differencewhat is diff between const let and var in jslet 26 const defferncejavascript let const vs varjavascript diff between const and varlet var and const in javascriptdifference between var let and const with examplewhat is the difference between var let and const in javascriptdiff between const let and varlet vs var vs const javascript tdzjavascript var vs let and constwhat is defferent between var let constdifference between var let and constant in javascriptdifference between var 2c let 2c constdifference between var let const in javascriptlet vs var vs constantdiference between const 2c var 2c letjavascript diff const let varvar vs let vs constlet vs const in jsvar 2c let 2c and const what is the differencediff between let var and constconst vs let jsdifference between const let vardifference between var 2clet 2cconst in javascriptvar const let differencejavascript let and constvar let const differenceconst differencewhat is the difference between var 2c let 2c and const 3fdiff between let and var and constdiff between let const and varwhat is the difference between const and let in jslet vs var vs constnodejs const vs var vs letwhy use let vs constdifference between let 2cvar and constdifference between const 2clet 2cvarthe diffrence between var let constdifference between var const and lethow let and const is differnce from var in jsdifference between let var and constwhen to use var vs letwhat is the difference between const and letlet const var differencedifference between let 2cconst 2cvardifferenv between let and constdifference of let 2cvar 2cconst 2cjavascript const vs letjavascript var vs let vs constwaht is difference bw let and constdiffernce between var 2clet and constwhat is diff between w let const and varjavascript let or constdifference betwwen const and let 3fdifference between let 2c const and var in javascriptdifference between var 2c let 2c constconst let var difference jsdifference in let const and varwhat is the difference between let var and constwhat is difference between var and let and const in javascriptdifferences between let var and constconst let and var differencedifference between var 2c let constjs let vs const vs varjavascript var 2c let and constdifferenfce between let 26 const javascriptwhat are the differences between let 2c const 2c and vardifferences between var let constdifference between let constant and var in javascriptdifference between const and let in javascriptwhat 27s the difference with let and constlet 2c var or constwhat is the difference let 2c var constts let vs var vs constjs var vs let vs constdifference between var let and const in javascriptdifference between let 2c var and constdifference in let const and var jsis let a const javascriptwhat is difference between let and const in javascriptdifference between var let and const in jslet vs var constvar vs let and constdifference between let and var and constlet var const differnce javascriptwhat is the difference between let const and varjavascript difference between const and letthe difference between var and const 26 letlet vs const in javascriptdifference between var 2c const and letjavascript var let const differencedifference between const 2clet and varconst vs let javascriptdifference between var let and const javascriptdifference between let and var and const in javascript difference between const and var in javascriptdifference between var const and letrdifference between const var letdifference between let and const object javascriptlet and const var diffdiff betwwen const var and letdifferenec between var let and constdifference between let var constwhat 27s the difference between let and constlet const vs varreact var vs let vs constconst vs let vs var javascriptdiffernce between var let and constjs difference between let and varlet var const difference javascriptfdifference between let var const in jsvar let and const diffrence in jswhats the difference between const and var in jsconst vs let vs var in jdifference between let const and var javascriptlet v var vs constdifference between let and constconst vs let vs var vs functiondifference among var 2c let and const in javascriptvar or let or constdifferenece between let and constconst let and var javascriptdifference between let and const in java scriptdifference between const and let javascriptjs difference const and letwhat is the difference among var 2c let 2c constdifference between var and let constjavascript const vs let vs var unterschiedwhat is difference between var and const and letdifference const var and let javasciptwhat is the difference between var 2c let 2c constdifference let and constvar vs let vs const typescriptdifference between const let and vardifference between const 2c let and vardifference in var let and constlet vs const vs var javascriptdifferene between let const nodedifference between let 26 constdifference between var 2c let 26 constwhat is the difference between let 2c var and const in javascript 3fdiff between let and constdifference between var let and constscope with var let constlet and const javascript difference performancedifference between let and var and const in javascript with examplewhat is the difference between var and const in javascriptin javascript 2c what is the difference between 60var 60 2c 60let 60 2c and 60const 60 3flet vs const in java scriptdifference between let 2c var 2c constlet vd constjavascript difference between const let and vardifference let var constdifference const let vardifferences between var 2c let 2c and constvar 2c let and constwhat is the different between const and letlet const var different in javascriptdiff between const and var in javascriptwhat are the differences between let 2c const and varlet and const js differencedifferences between var 2c let and const in javascriptdifference of const let and vardifference between const 2c var 2c and letwhat are the differences between var 2c let and constdifference between let 2c var and const in javascriptdiffrence between var let constlet var vs constwhat are the differences between variables created using let 2c var or const 3fconst vs var vs letdifference between const var and let in jslet and var and const in javascript let vs constant vs var in javascriptwhy const vs letdifference between const and var javascriptdifferences between const let and vardifference between const 2c var and let in nodejslet vs constdifference of var 2c const and letlet vs const what is betterdifference between let and const in jswhat are the differences between var 2c let 2c and constuse of let and const in javascriptconst in js meaning vs var lethwhat 27s the difference between const 2c let and varjs difference between var let and constdifference let var const javascriptdifference between const 2c let and var in jsvar vs cont vs letdifference between var 2c let and const with exampledifference between let 2c var and const in java scriptwhat 27s the difference between var let and constlet vs const 3fvar vs let const javascriptin javascript the main differences between let and var variable declarations aredifference between var let and const in typescript javatutodifference between const and letvar vs let vs const javascript memorydifference between const let var newdifference between let var const in javascriptjs difference between const and letdifference between variable and constant and let in javascriptlet vs var vs const javascript differencelet vs var vs const vs valjavascript let var const differencedifference between const let and var in javascriptlet vs const javascriptwhat are the differences between let 2c const and var 3fvar vs let vs const 2cdifferences of let 2cconst and varjavascript difference between let and vardifference const var letdifference between let and const javascriptjavascript difference between var and constdiffernece between let and constdifference between var and let in javascriptdifference between var and let and const in javascript