As the question says, which is more optimal and why?:
let carro = "subaru";
var carro = "subaru"
As the question says, which is more optimal and why?:
let carro = "subaru";
var carro = "subaru"
let allows variables to be declared by limiting their scope to the block, statement, or expression where they are being used. This differentiates the let expression from the var keyword, which defines a global or local variable in a function regardless of the scope of the block.
let vs var
When we use let inside a block, we can limit the scope of the variable to that block. Note the difference between a and var, whose scope resides within the function where the variable has been declared.
Source: Mozilla Developer
let
defines a local variable by limiting its scope to the execution block, expression, or statement it is in. It is a non-standard feature so it can cause problems in different browsers.var
defines a variable by limiting its scope to the function in which it is defined or to the global scope (if it is not inside a function), regardless of the block of execution in which it is executed.Several, but the most significant have to do with scope:
The idea (IMHO) is that you use
var
for things that are private to the module andlet
for things that are local to functions.