Javascript ES6+, using bracket notation to access module global variables [duplicate] - javascript

This question already has answers here:
In what scope are module variables stored in node.js?
(4 answers)
Closed 4 years ago.
Is it possible to access a global variable inside an es6 module using the bracket notation, basically doing something like this:
const GLOBAL = 'something'
console.log(this['GLOBAL']) // evaluates to undefined, of course
Doesn't seem to be working

Global constants do not become part of the widnow object, unlike var.
This declaration creates a constant whose scope can be either global or local to the block in which it is declared, but if left alone in the global, they do not become property of the window object.

Related

In JavaScript; does a let variable declared outside of a block get a global scope? [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)
Closed 2 years ago.
In JavaScript; does a let variable declared outside of a block become a global scope?
let letVariable = 2;
console.log(letVariable);
I don't know if I understand you correctly.
There is no "outside of a block", because even if you use it on the blank page, it is in the "main/global block" but only in the same script. its not available from other scripts.

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

Places where can I declare variables in JavaScript [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I am new to JS. I have the following question -
Is it right to say that I can declare variables only in global scope or inside a function/method only in JS and in no other type of Object which is not function/method?
I understand that properties can be defined in any object of Javascript but variables cannot be declared everywhere.
EDIT: I might not have right terminology to explain but in simplest words I need a list of places where i can write var x = .. in JS code. Please,I am not looking for scope of a variable.
you can the variable anywhere you want at the beggining of the program in the middle or if you need one in a function you can do it there too
Variables can be declared at top-level, function parameter lists, and statements in function bodies.
Object and array literals can contain nested functions, and you can declare variables inside these functions as well. But you can't declare variables directly in object/array literals; e.g. you can't write:
const foo_array = [
let a = 1,
let b = 2
];

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.

Scope of variables in a for loop in JS [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 7 years ago.
Consider the following for loop in Javascript.
for( var i=0;i<10;i++){ }
console.log(i);
If i run the code, I will get 10 in the console. Why is this? How is the scope of variable 'i' available outside for loop ?
In javascript, there is no block level scope prior to ES6 (with let), so any variable you declare will have a function level scope (if the declaration is inside a function) or global scope.
JavaScript before ECMAScript 6 does not have block statement scope;
rather, a variable declared within a block is local to the function
(or global scope) that the block resides within. For example the
following code will log 5, because the scope of x is the function (or
global context) within which x is declared, not the block, which in
this case is an if statement.
Variable scope

Categories

Resources