0

Why not "let" instead of "var" ?

Using "let" to declare/define variables is more flexible than using "var", infact using "var" is considered bad practice and deprecated.

11th Apr 2020, 6:46 PM
Hussaini Magaji
Hussaini Magaji - avatar
3 Respuestas
+ 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. Summary: Var => scope global Let => scope local
11th Apr 2020, 6:52 PM
Cmurio
Cmurio - avatar
0
"let is more flexible than using var": that's true. "using var is considered bad practice and deprecated": not exactly... The coders world is binary, some think so, and avoid using var, but others are not agree (less or more). Even if I have no doubt about the usefulness of let (and const, wich are not really constants in a way) against var, I mostly continue to use 'var' (that's not worth than 'let' if you know what you do) essentially for simplest backward compatibility, since a lot of mobile devices still not support it (android4 for example, wich is still present in the landscape -- at least mine ;P)... and a few for visually reason: with 4 spaces sized indentation, you could define variables on multi lines and align them on the next tabulation after the first line ^^ (and if I use let I want to benefit from const, as I should accept to not be compatible with my older devices, or I would choose to support more device and use only var)
11th Apr 2020, 9:24 PM
visph
visph - avatar