0

What’s the difference between variables?

In JavaScript, what’s the difference between “let” and “var” when creating variables? I have heard web developers use them interchangeably. Thank you all responders!

13th Nov 2019, 6:47 AM
CodeBoi!
6 Answers
+ 4
Let defines a variable inside a block of code so that that variable is not accessible outside of that block. For example: if (condition){ let a = 3 } console.log (a) // gives an error because a is not defined outside of if block. But with var you define a globally accessible variable.
13th Nov 2019, 7:08 AM
Qasem
+ 3
https://code.sololearn.com/WdQOWA45wJyR/?ref=app
13th Nov 2019, 7:23 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 2
among themselves, they differ in scope. Var has a global and let bounded (within a function) for example
13th Nov 2019, 7:10 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Thank you to all who replied to my question! I believe I understand "let" and "var" now.
18th Nov 2019, 11:16 PM
CodeBoi!