why let keyword does not work with eval() [duplicate] - javascript

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()

Related

Javascript global scope with const vs var [duplicate]

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?

How do we explain javascript hoisting here? [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Why is no ReferenceError being thrown if a variable is used before it’s declared?
(3 answers)
Closed 5 years ago.
I wonder why the two code snipped below generate two different results as in reference below the two have to be the same as were are in global scope and placing var would not make a difference.
I was under the impression due to what explained below and the Javascript hoisting the two generate same output, but don't! why?
What is the purpose of the var keyword and when to use it (or omit it)?
console.log(a)
var a = 90;
// undefined
vs
console.log(a)
a = 90; // no var keyword
// Uncaught ReferenceError: a is not defined
When you put var keyword, what it does is it moves the var part as a declaration on that scope and makes it undefined. In the first case, with var:
console.log(a)
var a = 90;
// undefined
Gets rewritten as:
var a = undefined;
console.log(a)
a = 90;
// undefined
So, you get the undefined here, vs. your second case has it the same way, where there's no declaration or key in window object named a. Hope this is understandable.
The point here is, when you use a var keyword, immediately that's defined as undefined at the top of the scope before anything.

Difference between local and global variables in javascript? [duplicate]

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 6 years ago.
I noticed that in JavaScript, Inside a function some times a variable created without mentioning var before it.
For example :
function myfunction() {
x = 10;
var y = 10;
}
what is the difference between these both?
function myact() {
x = 20;
var y = 10;
console.log(x);
console.log(y);
}
myact();
console.log(x);
//y not available here
var is used for declaration. So, assigning without declaration will simply give it global scope meaning: it will first search if it is available in any scope stack above, if not creates the variable implicitly in global scope and assigns.
JS Docs says:
The scope of a variable declared with var is its current execution
context, which is either the enclosing function or, for variables
declared outside any function, global.
Assigning a value to an undeclared variable implicitly creates it as a
global variable (it becomes a property of the global object) when the
assignment is executed.

Javascript variable declaration, why is this legal? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
I just encountered this variable declaration syntax for the first time.
var myVar = (function() {
return 1;
})();
My 2 main questions are..
What is it called, and why is it legal?
Forgive me if this question has been asked before, I tried searching around but I have no idea what this notation is called so I wasn't able to find anything.
Also, I should add, what is the function of the 2 sets of parentheses? The first of which encloses the function, the second of which is empty.
Self executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.
The following code:
var same_name = 1;
var myVar = (function() {
var same_name = 2;
console.log(same_name);
})();
console.log(same_name);
produces this output:
1
2
By using this syntax you avoid colliding with global variables declared elsewhere in you javascript code.
I am not sure what this is called, other than defining an anonymous function and immediately invoking it.
It is perfectly legal, because
Defining an anonymous function is legal.
Invoking it and assigning the return value is also legal.
The end result is that myVar = 1.
This is an anonymous function (also called lambda function) that is being executed immediately with its return value (1) being assigned to a variable (myVar). It's legal because the specification says it is. It is a very common feature in many languages.

why without var? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between using var and not using var in JavaScript
seems stupid question but just curious.
num = "hello";
alert(num);
why is this possible I didnt initialize the num variable here.
num = "hello";
instead of
var num = "hello";
var means "Scope this variable to this function" not "Make it possible to use this variable".
If you leave var off then it will use the variable in the next scope up from the function. If that scope doesn't have the var yourVar then it will keep going up the scope chain until it becomes a global and dangles off the window object (assuming a browser or another JS environment in which the default object is window).
(This changes in strict mode, which you are presumably not using.)
Without the var keyword the variable is created at the global scope (in browsers under window).
If you add the var you just assign the variable to the current scope, which in most cases will be a function scope.
In general it will be better to use a function scope in order to not pollute the global scope and prevent conflicts between multiple variables of the same name!

Categories