0
What is the difference between var and let in Javascript?
3 Answers
+ 2
Before understanding the difference between var let first you have to know about SCOPE in JavaScript.
Scope defines that where variables and functions are accessible inside code.
There are two types of scope in JavaScript:
ā¢ Global Scope
ā¢ Local Scope
When you begin writing code in JavaScript, you are already in the GLOBAL SCOPE. Anything written in the global scope is accessible anywhere in your JavaScript code but in LOCAL SCOPE variables are only visible and accessible within their local scopes (where they are defined i.e. inside a function).
Each function in JavaScript creates a LOCAL SCOPE and the varibles inside it are accessible within the function.
Now you have some understanding osf Scope in JavaScript now lets jump toward the main thing the difference b/w var and let.
The difference is that the var is function scoped and let is block scoped which means variables declared with var is defined throughout the program as compared to let.
+ 2
Also read this article for better understanding with some examples inside.
āWhen to use var vs let vs const in JavaScriptā by Tyler McGinnis https://link.medium.com/Ya6ixqD6JT
+ 2
Var is global variable.
Let is local variable.