This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
ES5 introduced use of let, but I don't understand if I already have var then why i need to use keyword let in javascript.
Variables declared with let and const eliminate this specific issue of hoisting because they’re scoped to the block, not to the function. Previously, when you used var, variables were either scoped globally or locally to an entire function scope.
If a variable is declared using let or const inside a block of code (denoted by curly braces { }), then the variable is stuck in what is known as the temporal dead zone until the variable’s declaration is processed. This behavior prevents variables from being accessed only until after they’ve been declared.
Related
This question already has answers here:
Are variables statically or dynamically "scoped" in javascript?
(7 answers)
What is the scope of variables in JavaScript?
(27 answers)
What is lexical scope?
(21 answers)
Lexical Scope in JavaScript
(4 answers)
Closed 8 days ago.
I am working on a large library and I want to reduce conflicts in the code as much as possible. I have a function like this:
((privateArgument) => {
var transformed = privateArgument.someProperty;
userCallback(); // This should only have access to transformed
})({someProperty: 'Hello, world!'});
I should mention that I tried to simplify the example as much as possible to illustrate my intention. In reality, I have no control over the value passed to the function ({someProperty: 'Hello, world!'}).
And an example of the userCallback:
() => {
console.log(transformed); // Can be used here freely
console.log(privateArgument); // Can unfortunately also be accessed
}
I have tried the following:
Setting privateArgument = undefined: breaks access to transformed and does not throw the native "undefined" error
Overriding the privateArgument by defining a new let privateArgument and scoping the userCallback with {}: gives me an error, saying that I cannot redefine it
Passing transformed as an argument to userCallback: does nothing to address the issue
Please note that I am specifically asking about arguments, not regular variables such as var and let. I am aware that the issue regarding normal variables has already been addressed in other questions. However, the answers to those questions are not helpful for this particular issue.
If anyone knows of a scoping mechanism I can utilize for this or how to completely get rid of the privateArgument (so that it throws the native error and can be re-declared in the userCallback) it would be greatly appreciated.
This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed last year.
When I search for difference between let and var in javascript it says variable declared by let can't be redeclared but variables declared by var can be redeclared but does this line mean?
Well hello Vinay Singla the thing is when you use let you do not have to redeclare it.
Otherwise an error will pop and a new let is very similar when you declare variables in python .
Code you wrote will cause an error
This question already has answers here:
Define const variable using eval()
(2 answers)
Closed 4 years ago.
function foo(str, a) {
eval( str );
console.log( a, b );
}
foo( "var b = 3;", 1 );
This works just fine, but when we use let instead of var, it does not work. Why?
Because eval introduces a new block of code. The declaration using var will declare a variable outside of this block of code, since var declares a variable in the function scope.
let, on the other hand, declares a variable in a block scope. So, your b variable will only be visible in your eval block. It's not visible in your function's scope.
More on the differences between var and let
EDIT : To be more precise eval + let do, in fact, create a Lexical Environment. See #RobG response in Define const variable using eval()
This question already has answers here:
A javascript 'let' global variable is not a property of 'window' unlike a global 'var' [duplicate]
(1 answer)
Do let statements create properties on the global object?
(5 answers)
Closed 5 years ago.
var name = 'John';
console.log(this.name, document.name, window.name, name);
const meme = "Bruce";
console.log(this.meme, document.meme, window.meme, meme);
Output:
John undefined John John
undefined undefined undefined "Bruce"
Is global scope differently defined for var and const? I thought the only difference would be the const is immutable.
Yes, window scope and variables defined using var is different from the scope of variables declared using const and let.
See also Is it possible to delete a variable declared using const?, Why does .then() chained to Promise.resolve() allow const declaration to be reassigned?
This question already has answers here:
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Closed 9 years ago.
Why do we use the keyword: 'var' when the code works very well without using it.
JavaScript:
window.onload = compare;
function compare() {
cool = "45";
alert(cool +1) // I can alert it or do anything else without using 'var'
}
Here, a string is stored in 'cool' and I can do anything with it as if it was a variable. Isn't it a variable here? If it is, then what difference does the keyword 'var' makes?
When you omit the var keyword you're doing an "implicit declaration". This is not recommended as this type of declaration has global scope. If you want a locally scoped variable you need to use the var keyword.