Why global let variables not added to window object of browser? [duplicate] - javascript

This question already has answers here:
why don't const and let statements get defined on the window object [duplicate]
(2 answers)
Closed 3 years ago.
Consider the code below which I am running in my chrome browser console:
var x = "hello";
let foo = function(){
console.log("hi");
};
console.log(x); // hello
console.log(foo()); //hi
console.log(window.x); // hello
console.log(window.foo()); // Error
If I had used var foo =... instead of let foo = .. then this would have worked. Why ?

This is by design:
let allows you to declare variables that are limited to a scope of a block statement, or expression on which it is used, unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope. The other difference between var and let is that the latter is initialized to a value only when a parser evaluates it (see below).
Just like const the let does not create properties of the window
object when declared globally (in the top-most scope).

Related

What is the scope of my variable when I use let? [duplicate]

This question already has answers here:
Do let statements create properties on the global object?
(5 answers)
A javascript 'let' global variable is not a property of 'window' unlike a global 'var' [duplicate]
(1 answer)
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
Im new in javascript and in programming.
If I declare a variable with var I see that my variable is declared globally and my variable is inside the window object.
Example:
var element1 = 1;
window.element1; //This returns 1
But when I use let I can't access my variable using the window object.
Example:
let element2 = 1;
window.element2; //This returns undefined
So, where has been my variable element2 been declared?
What is the scope of element2 ?
You are confusing scope and automatic attachment to the default object.
If you use let or var outside of any block, function, or module then the scope will be global.
var will also attach a property of the same name to the default object (which is window in the case of JS running in a browser).

If statement vs function blocks for var keywords [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
My question is why is a variable (when using var keyword) accessible outside the if statement block, but not outside the function block? Consider this code:
let age = 30;
if (true){
let age = 40
console.log('inside', age) //logs inside, 40//
var name = 'shaun'
}
console.log('outside if loop', age, name) //logs outside if loop, 30, shaun //
function test (){
var xyz = 'ashley';
console.log(xyz)
}
test ()
console.log('outside function', xyz) //throws an error//
Thank you for your time. Any insights would be appreciated!
Because of the current context of execution. The context in which values and expressions are "visible" or can be referenced. If a variable or other expression is not "in the current scope," then it is unavailable for use. Scopes can also be layered in a hierarchy, so that child scopes have access to parent scopes, but not vice versa.
A function serves as a closure in JavaScript, and thus creates a scope, so that (for example) a variable defined exclusively within the function cannot be accessed from outside the function or within other functions.
Also ES6 does have block scope for variable definitions with the let and const keywords but not with var.
Here is an article on Wikipedia about Scope.

Why can I declare a javascript var multiple times? [duplicate]

This question already has answers here:
Declaring a Javascript variable twice in same scope - Is it an issue?
(5 answers)
Closed 4 years ago.
Why am I able to declare a var multiple times? I would expect an error.
This code raises an error:
let a = true;
let a = false;
Uncaught SyntaxError: Identifier 'a' has already been declared
Why doesn't this raise an error too?
var b = true;
var b = false;
Expected: Uncaught SyntaxError: Identifier 'b' has already been declared
It's because there's variable hoisting with var, but not with let (or const for that matter).
So that means that each time you use var, it'll essentially cancel out the previous operations because to the JavaScript interpreter, your first code looks like:
var b;
b = true;
b = false;
But this doesn't work with let or const because let and const are block scoped, whereas var is function scoped.
var gets hoisted; duplicate variable names are ignored. To the interpreter, your second snippet looks like:
var b;
b = true;
b = false;
In contrast, let is not hoisted, so duplicate declarations are forbidden.

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.

Javascript: this.varName = "" vs var varName = "" [duplicate]

This question already has answers here:
Difference between this and var in a function
(5 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 9 years ago.
I'm new to JS so noob question:
I see functions which define vars inside them:
function functionName(){
this.something = "";
};
If I understand correctly, something is a local variable? Why is it being defined as this.something = '' as opposed to var something = ''?
Is there any difference, and if so what is it?
It sets the attribute something of this. What this refers to depends on how functionName is invoked, but typically it's an object created with new:
var foo = new functionName();
console.log(foo.something);
If you'd use var something inside the function, the variable would be inaccessible from outside, so you couldn't do foo.something in the above example.
var someThing
is a local variable - meaning it exists within the scope of the current block
this.someThing
is an instance variable - meaning it belongs to the object and is visible in all methods of that object.

Categories

Resources