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

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

Related

In Javascript let can be updated but cannot be re-declared, const cannot be either, what exactly is the difference between updated and re-declared? [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
What is the difference between 'let' and 'const' ECMAScript 2015 (ES6)?
(10 answers)
Closed 7 months ago.
Let and const, both block scoped, but let can be updated into the scope while const cannot, though neither can be re-declared into it. Objects assigned to a variable using const are still mutable, for example values in an array. Where my confusion lies is the concept of updating vs re-declaring a variable with these two, and how would it look in code. I'd appreciate any further explanation.

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

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.

Unexpected behaviour of JavaScript with let and var keyword [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Are variables declared with let or const hoisted?
(7 answers)
Closed 4 years ago.
When i am using var keyword to declare a variable then JS engine is assign default value to "message" at creation stage
console.log(message); //undefined
var message = "My message";
but with let keyword
console.log(message); //message is not defined
let message = "My message";
why this unexpected result or it is something changed in ES6?
This is because of the creation of Temporal dead zone with let
let bindings are created at the top of the (block) scope containing the declaration, commonly referred to as "hoisting". 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.

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.

Categories

Resources