Javascript vars | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Javascript vars

Hello guys i started learning Javascript recently... When i got to how to declare variable in if... I understood var and hoisting (hoisting means geting the declaration (just declaration) and bringing it in at the top) but i don't seem to understand let... Its a block scoped means it can be used only used in curly braces (like if statement and loops but not functions)... So,what does that mean... Why shoud we use it?.. Why shoud we avoid using var instead of let... Can somone explain please.. With some examples... I understand better with examples... (cause my English ain't that good)

11th Aug 2019, 5:28 PM
Amirabbas
Amirabbas - avatar
3 Answers
+ 5
Hello! Var declarations are, as you may know, function scoped; which is really stupid. If you look at any other language, variables by default are block scoped; and so is let. It doesn't mean it can only be used in blocks, but that it only exists inside a block. It can be pretty confusing though. But, I made a code just for these kinds of things; it might be worth it to check it out and gain more information. https://code.sololearn.com/WV1fQUrNkt25/?ref=app
11th Aug 2019, 5:42 PM
Airree
Airree - avatar
0
Airree thanks alot
11th Aug 2019, 7:41 PM
Amirabbas
Amirabbas - avatar
0
;)
11th Aug 2019, 7:41 PM
Airree
Airree - avatar