0
When to use var const and let
When to use var const and let in js and other programs if it's has
6 ответов
+ 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
+ 3
That depends on the scope: local vs. global. If you don't need to access it in a different scope, keep it local.
+ 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
}
+ 1
Lisa
Oh tnx
+ 1
CutieRei
Hi
Thank you🙏🙏🙏
0
Lisa
Tnx
I knew this but my question is when tu use them
Like do I have to use var or let