How are 'let' variables accessed in devtool's console? [duplicate] - javascript

This question already has answers here:
Do let statements create properties on the global object?
(5 answers)
Closed 2 years ago.
If you open chrome devtool and enter the following:
// The iife is irrelevant
let r = (() => { return 2; })();
and then evaluate r you'll see:
r
2
but window.r and globalThis.r both return undefined. I know let is block scoped but where is the r parent object? What block am I in, in the devtool that I can access r directly but it's not on any global object?

Apologies, I'm on my phone, so can't really verify. But I'm pretty sure there is a 'scope' tab as part of the Dev tools. If you're debugging I'm pretty sure you can see variables there in the current scope. But it's not available in the normal console
https://developers.google.com/web/tools/chrome-devtools/javascript#scope
Edit, just re-read your question, and this doesn't really answer your question description, but does kind of answer your question title. So probably going to leave the answer here for future ref

The code is evaluated in global scope.Variable "let" declared Globally (outside any function) have Global Scope.

Related

JavaScript hoisting confusion regarding let [duplicate]

This question already has answers here:
What is the temporal dead zone?
(3 answers)
Closed 2 years ago.
a= 10; console.log(a); let a; => Cannot access 'a' before initialization
above code gives error for initialization, even if a is declared before use. Please explain how this gives an error if a is already hoisted and get declared "a=10" before using it.
That also means if a variable is declared with let or const, hoisting doesn't have any practical impact ?
if I run below code
let a; console.log(a); => undefined
in this case I am able to print a i.e. undefined, without initializing it.
What is happening in core ?
When you type:
a=10
then you are defining a global variable. It is deprecated and not recommended style of coding in almost all languages.
To prevent declaring global variables es6 introduced let and const.
Now when you are using let or const your interpreter checks that when you typing
let a;
you do not have a used before this declaration. This is the reason for the error.
Update:
You typed:
let a; console.log(a); => undefined
it is ok because a is declared, memory is reserved, the symbol a is registered, but the value is not assigned, so when you evaluate the value assigned to symbol a it is undefined.
Please compare it with case:
console.log(c);
You will get an error:
Uncaught ReferenceError: c is not defined
because the symbol c was never declared.
Now when you will assign values to a it will be done in local scope, but when you will assign values to c it will create global variables.
To improve your understanding please read about all methods of defining variables and look at all edge cases. A great article is under the link:
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/

Why does it throws reference error for b? [duplicate]

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
Recently I attended a interview for Frontend development, The interviewer came up with a problem which made me doubt my confidence on javascript. The question is
function some() {
console.log(a) // undefined
console.log(b) // Reference Error: cannot access b before initialization.
var a = 10;
let b = 15
}
I understood hoisting is happening here, but i am not sure why b throwed reference error. Could anyone please explain ?
Unlike variables declared with var, which will start with the value undefined, let variables are not initialized until their definition is evaluated. Accessing the variable before the initialization results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Logging Global variable inside the function Block immediately before redeclaration going undefined: Should not it take the Global Variable Value? [duplicate]

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
Closed 5 years ago.
let pages =100;
let pageFunction=()=>{
console.log(pages);
const pages = "20";
//console.log(pages);
}
pageFunction();
Output:
Uncaught ReferenceError: pages is not defined
at pageF (:3:13)
at :1:1
According to Mozilla "The let statement declares a block scope local variable, optionally initializing it to a value." Also "At the top level of programs and functions, let, unlike var, does not create a property on the global object."
See
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/let for more information.
This means that in the code you gave you effectively create an implicit global variable pages, as let pages is not really global, but restricted to the scope it is in.

es6 Temporal dead zone [duplicate]

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
Closed 7 years ago.
I know Temporal dead zone in es6. But I was confused about the procedure of the following code.
Javascript is a kind of Interpretive language.How does it knows there is a s will be declared in this block behind rather than use the s outside this block.
In an other word , what is the procedure of the following code ?? I am new here, please help me.
'use strict'
var s = 1;
if (true){
console.log(s);
console.log("AAA");
let s = 2;
}
Javascript code is run in multiple passes. A first pass will go through and deal with all the declarations and assign them to their scopes.
That's how it "knows" that a left hand side reference to "s" will be declared via let for the scope of that if{} block, even though the non-hoisted let hasn't actually declared it yet.

This binding in node.js [duplicate]

This question already has answers here:
In what object does Node.js store variables?
(3 answers)
Closed 8 years ago.
I am puzzled by some node.js behaviour, which is different fron Google Console.
Here is a simple code
var t = "GLOBAL";
var objet = {
t : "LOCAL1",
test : function() {
console.log(this.t,t);
}
};
var objet2 = {
t : "LOCAL2",
test : objet.test
};
var test = objet.test;
objet.test();
objet2.test();
test();
This code yiels different result.
In node.js, i have these results :
LOCAL1 GLOBAL
LOCAL2 GLOBAL
undefined 'GLOBAL'
In chrome console :
LOCAL1 GLOBAL
LOCAL2 GLOBAL
GLOBAL GLOBAL
I thought that calling directly the function binded to test.t, this would be binded to the global scope, it is the case in chrome, but node in node.js.
Note that if i remove the varin the first line, the node.js version give the same result.
So what is going one ? It seems that there is a scope in node.js that i am missing ?
Does somebody have a clue ?
In NodeJS, the default scope is that of the module. Global scope is even bigger than the module, which is what this refers to. In Chrome, the global scope is window, which is what this is.
See the module documentation and more importantly, globals.

Categories

Resources