var vs let vs const typescript

Solutions on MaxInterview for var vs let vs const typescript by the best coders in the world

showing results for - "var vs let vs const typescript"
Julián
23 Sep 2019
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.
Alienor
04 Feb 2018
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();
Emil
24 Feb 2020
1let num1:number = 1; 
2    
3function letDeclaration() { 
4    let num2:number = 2; 
5
6    if (num2 > num1) { 
7        let num3: number = 3;
8        num3++; 
9    } 
10
11    while(num1 < num2) { 
12        let num4: number = 4;
13        num1++;
14    }
15
16    console.log(num1); //OK
17    console.log(num2); //OK 
18    console.log(num3); //Compiler Error: Cannot find name 'num3'
19    console.log(num4); //Compiler Error: Cannot find name 'num4'
20}
21
22letDeclaration();
23
queries leading to this page
difference between const and let and varconst vs let globaljavascript const vs let vs var unterschiedwrite the difference between var let and const with code examplesvar vs cont vs letlet vs var vs const jsconst vs var vs letlet vs constwhat is difference between let const vardifference between var and let and constwhat is difference between const vs let vs varvar let vs constjs let vs constts let vs var vs constdifference between let 2c var and const in java scriptconst vs var typescripttypescript const keyworddifference const var letlet vs const vs var faster to calldifferenec between var let and constcreated a ts variables with let var or constdifference between const and var in typescriptconst vs let in jsdifference of var let and constvar vs let vs const examplewhen to use const vs let typescriptlet vs var vs constanttypescript let or var or constlet vs var vs const vs valwhy is let used more in typescriptlet vs const vs varwhat is the difference between let and const in typescriptdiff c3 a9rence let var constlet vs var vs const node jsvar or const expressdifference between var let and const in typescriptconst vs let vs var vs functionjavascript var vs let vs constwhen to use let vs consttypescript constlet vs const javascript 22 3cconst 3e 22 typescripttypescript keep const over varlet vs const in jslet vas const vs vardifference between var and let and const with examplewhen to use var vs let constwhat is difference between let var and const in javascriptconst and let differencedifference between let 2c const and var javascripttypescript let vs varconstant var tstypescript var vs let vs constwhat is the difference between var let and const latest newsdifference between var 2clet 2cconst in javascriptconst vs let jswhat is the scope of const in typescriptdifference between let const and varwhat is difference between var const letconst vs let vs var typescriptwhat are the differences between variables created using let 2c var 2c and constjs var vs let vs constdifferences of let 2cconst and varconst let var differencewhen to use let vs var vs constdifference between var and let constlet vas const vs var exampledifference between var let and const javascriptdifference between var let and const with examplevar let const differencejavascript const vs let vs varlet vs const in javascriptvar 2c let 2c and const what is the differencelet vs var vs const in javascriptvar vs let vs const in javascriptlet vs const referencediffernece between var let constvar const let differencelet and const var diffdifferences between var let and constvar vs let vs const javascriptlet var vs constdifferences between var let and const javascriptlet const var differencevar vs const vs letconst vs let 27let 27 declarations can only be declared inside a block tsangular var vs let vs constdifference between var 2c let and const with examplelet var and const differencejavascript let vs constlet vs var vs const typescripttypescrip let vs const vs varwhat is the difference between const var and letdifference between let 2cvar and constdifference between let const vardifference between let var const in javascriptdifference between var let and const in javascriptlet or var typescriptdiffrence between var 2c let constdifferences between let const and vardifference between var let constlet vs var vs const javascriptwhat does let and const in typescriptjs difference between var let and constconst vs val vs letdifference between var 2c let 2c constdifference between const var and letlet vs const vs var typescriptwhat is difference between let var and constdifference between let 2cvar and const in typescriptis let keyword valid in tsdifference among var 2clet 2cconstdefferent between const let varwhat is the difference let 2c var constdifference between const let vartypescript difference between const and letwhat 27s the difference between var let and constlet vs const vs var in javascripthow to let type script know a variable is definedconst in js meaning vs var letjavascript var vs const vs letdifference between const var and let in jsjs let vs var vs constvar let and const in typescriptjs var vs const vs letvar vs constreactlet statement with type tsdiff between let var and constdifference between var 2c const 2c lettypescript declare a const variable without valuedifference between const var and let in javascriptdifference between var const and letdifference between var const 26 let in javascriptvar or let or constconst vs let vs var javascriptdifference between var const and letrstatic vs const vs let vs varlet vs var vs const in jsdifference between let and var and const in javascriptdifferences between var 2c let and const in javascriptdifference between const let and var in javascriptwhat is the difference between let var and const in javascriptdifferent between let and const and varvar vs condt vs letwhat is the difference between var let and constwhat is defferent between var let constwhat are the differences between let 2c const and var 3flet and var and const differencedifference between let 2c const and var jsdiff between const let and varconst in typescriptvar let and const differenceconst vs let in typescriptdifference const let vardiff between let var const in typescriptdifference between var let and const in jsdiffernce between var 2clet 2cconstwhat is the difference between var let constdifference var let constdifference between let and const and var what is difference between const let and var typesdifferance between var let constwhat is difference between var let and const in javascriptlet vs const jsjs const vs letdifference between let and constdifference between var 2c let and constvar vs const y letdifferenv between let and constwhat is the difference between let const and vardifferenc between var let and constdifference in var let and constconst declarations must be initialized typescriptwhat is the difference between const 3f and const 21 in typescriptwhat is the difference between vconst 3f and const 21 in typescriptvar 2c let and consttypescript const vs let runtimedifference between const var and let javascript what are the differences between var 2c let and constdifference between var const let in javascriptdifferences between declaring variables using var 2c let and constdifference between const 2clet and varconst vs let javascriptdifference between let and const in typescriptjavscript const vs varlet vs var vs const tyoescriptlet vs var consttypescript const variableconst typecriptdiffernce between var let constjavascript let vs var vs constdiffrence between let const varvar vs let vs const typescriptdifferece between var 2cconst 2cletdifference between const 2clet and varwhat are the differences between variables created using let 2c var or const 3fdifference between var let constdiff between let const and vardifference between var let and const in typescript javatutoreact var vs let vs consttypescript let vs consttypescript const vs letdifference between let var and constdifference between var let and constwhat are the differences between var 2c let and constdifference between let var const in jswhy const vs letconst var let differenceconst vs var vs let javascriptvar vs let vs constnodejs const vs var vs letwhat are the differences between var let and const typescript letlet vs var vs const speedjs const vs var vs letdiff let vs var vs constlet vs const in typescriptdifference between const 2c let and var in jsdifference between const and letdifferent between let const and vardifference between let 26 constjquery cosnt vs vardiffrence in let var constvar vs let and constjavascript difference between var let and constwhy use let vs constconst vs var vs let in javascriptjavascript let vs const vs varlet vs const 3fwhat is the difference between const let and varwhat is the difference with const var letconst typescriptdifference of var 2c const and letjavascript const vs letdifference of const let and varconst vs let vs anyvar vs let vs consttypesccript consdifference between const 2c let and varlet const differencedifference between const let and vardifference between var 2c let constconst vs var vs lettypescript const vs let vs vardifferences between const let and varjavascript const vs var vs letlet vs var vs const javascript differencejavascript difference between let var constlet vs const vs var javascripthow to use let in typescriptdiffernce between var 2clet and constlet vs var vs constlet v var vs constdifference between let and var and const in typescriptvar vs let vs const in jsjavascript difference between const let and vardifference between let 2cvar and const in type scriptlet vs const vs var in jstypescript var vs letdifference between let var consttypescript constant variablelet var as type 3dconst let and var differenceconst vs let vs varconst var let typescriptdifference between let and var and const in javascript with examplewhen to use var vs letwht is difference between let 2c const and varlet vs const vs var vs freezetypescript changes let in vardifference between let var and const in jslet const vs vardiff between var let constjs let vs const vs vardifference between var and let and const in javascriptconst javascript vs letlet vs const what is betterdifference between const and let in typescriptdifference between var 2clet and constdiff between var let and constdifference let const varlet vs const typescripttypescript when to use const vs lettypescript let vs var vs constlet and const differencevar vs let vs const typescript