What is the difference between let and var? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What is the difference between let and var?

in javascript i alway confused about the two keywords 'var' and 'let' could anybody explain in detail

7th Mar 2018, 11:02 PM
Jalal Uddin
Jalal Uddin - avatar
2 Answers
+ 11
When used outside of a function, both var and let create global variables, which are recognized throughout the entire code. When used within a function, var lasts within the whole function, while let lasts within its code block (any code enclosed in {}). Ex: If you use var inside a for, that's inside a function, it lasts for the whole function. If you use let instead, it will only last within the for, and can't be used throughout the function.
7th Mar 2018, 11:19 PM
Tamra
Tamra - avatar