+ 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
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.