difference let and var
------------------Differences------------------ var let Global Scope Yes No Can be changed outside statement where made in Yes No Block Scope No Yes
js var vs let
let = 10 // accessable only in defined scope var = 10 // accessable in the function it is declared in (global variable in a function)
what to use let vs var js
/* DIFFERENCE BETWEEN LET AND VAR */ //LET EXAMPLE { let a = 123; }; console.log(a); // undefined //VAR EXAMPLE { var a = 123; }; console.log(a); // 123
difference between let and var in javascript
/* var and let are both used for variable declaration in javascript. var is function scoped. let is block scoped. */ // i IS NOT known here // j IS NOT known here // k IS known here, but undefined // l IS NOT known here function loop(arr) { // i IS known here, but undefined // j IS NOT known here // k IS known here, but has a value only the second time loop is called // l IS NOT known here for( var i = 0; i < arr.length; i++ ) { // i IS known here, and has a value // j IS NOT known here // k IS known here, but has a value only the second time loop is called // l IS NOT known here }; // i IS known here, and has a value // j IS NOT known here // k IS known here, but has a value only the second time loop is called // l IS NOT known here for( let j = 0; j < arr.length; j++ ) { // i IS known here, and has a value // j IS known here, and has a value // k IS known here, but has a value only the second time loop is called // l IS NOT known here }; // i IS known here, and has a value // j IS NOT known here // k IS known here, but has a value only the second time loop is called // l IS NOT known here } loop([1,2,3,4]); for( var k = 0; k < arr.length; k++ ) { // i IS NOT known here // j IS NOT known here // k IS known here, and has a value // l IS NOT known here }; for( let l = 0; l < arr.length; l++ ) { // i IS NOT known here // j IS NOT known here // k IS known here, and has a value // l IS known here, and has a value }; loop([1,2,3,4]); // i IS NOT known here // j IS NOT known here // k IS known here, and has a value // l IS NOT known here
difference between "let" and "var"?
In simple words 'var' is function scoped and 'let' is block scoped