0

When to use var const and let

When to use var const and let in js and other programs if it's has

27th Nov 2021, 7:43 PM
Morteza Ayashi
Morteza Ayashi - avatar
6 Respuestas
+ 5
You may want to review lesson 56 of the sololearn JavaScript course. in short: var is global and value can be changed const is global and cannot be change after initializing let is local
27th Nov 2021, 8:03 PM
Lisa
Lisa - avatar
+ 3
That depends on the scope: local vs. global. If you don't need to access it in a different scope, keep it local.
27th Nov 2021, 8:12 PM
Lisa
Lisa - avatar
+ 1
precisely: var is a keyword to declare a variable that is available to top level local group and its children like function b inside a function a, if variable declared in function b it be available to function a but not globally unless defined outside of any group like not in wrapped in any brackets const on the other hand works like let but its immutable so you can't change it after its been assigned a value let works only in its own local group and its children similar to function b in function a, but variable declared in b as let won't be accessible in function a examples: function main() { if (1==1) { var a = 1 let b = 1 console.log(a) console.log(b) } console.log(a) // 1 console.log(b) // ReferenceError because b is a variable declared as "let" inside the if block }
27th Nov 2021, 8:13 PM
CutieRei
CutieRei - avatar
+ 1
Lisa Oh tnx
27th Nov 2021, 8:20 PM
Morteza Ayashi
Morteza Ayashi - avatar
+ 1
CutieRei Hi Thank you🙏🙏🙏
27th Nov 2021, 8:21 PM
Morteza Ayashi
Morteza Ayashi - avatar
0
Lisa Tnx I knew this but my question is when tu use them Like do I have to use var or let
27th Nov 2021, 8:09 PM
Morteza Ayashi
Morteza Ayashi - avatar