What is the difference between var and let in Javascript? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

What is the difference between var and let in Javascript?

24th Jan 2019, 3:45 PM
Zeeshan Laskar
Zeeshan Laskar - avatar
3 Réponses
+ 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.
24th Jan 2019, 4:17 PM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
+ 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
24th Jan 2019, 4:18 PM
Gurpreet Jhamat
Gurpreet Jhamat - avatar
+ 2
Var is global variable. Let is local variable.
24th Jan 2019, 4:35 PM
Андрей
Андрей - avatar