Questions tagged [javascript-scope]
29 questions
1
vote
2 answers
Global variable declaration returns undefined, any reason for this
When I log input to the console, it returns undefined but when put inside a the anonymous function, it logs to the console as expected
I had tried the below code, and expected that the inputValue to be logged to the console on click of the…

Samuel666
- 11
- 1
1
vote
1 answer
Javascript in Node.js shows bizarre behaviour involving functions and block scopes
Can anyone make sense of the difference in behaviour between the two snippets described below?
Snippet #1
{
function f() {return 1}
f = function() {return 2}
function f() {return 3}
}
console.log(f()); // yields 2
Snippet #2
{
…

pdlaf
- 21
- 2
1
vote
0 answers
How variable scopes work in higher order functions in javascript?
I will use a debounce function as example.
const debounce = (func, wait) => {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
…

entropyfeverone
- 1,052
- 2
- 8
- 20
1
vote
3 answers
Javascript variable is not updating inside function
let number = 100
function change(number) {
number = number * 10;
}
change(number);
console.log(number);
The above code outputs 100, whereas
let number = 100
function change(blah) {
number = number * 10;
}
change(number);
…

Thamjith Thaha
- 45
- 2
- 13
1
vote
0 answers
Redeclared variable inside the block using let keyword and accessed before it but gives error
var a=10;
{
console.log(a);
let a =20 ;
}
ReferenceError: a is not defined
Why it is giving the reference error even a is declared using var which should have its scope inside the block too ?

Akshay Singla
- 25
- 6
1
vote
1 answer
Defining global variables in TypeScript for WeChat mini program
Dear StackOverflow users,
I made a module for both browser and WeChat mini program.
That module is written in multiple TypeScript and is compiled to one minified file.
In that module, I need to use DOM/BOM APIs such as document, 'window,…

DumTux
- 668
- 1
- 9
- 24
1
vote
2 answers
Access variables declared inside JavaScript switch statement from outside
Why does
switch ("string") {
case "string":
const text = "Hello World!"
break
}
console.log(text)
return error: Uncaught ReferenceError: text is not defined ?
I don't understand why the variable text returns undefinded.

Leonard Niehaus
- 500
- 6
- 16
1
vote
3 answers
changing value in prototype does not work in JavaScript
I am new in JS, lets have a look on my code bellow.
I want to change value of legs property of mouse to 2 while using proto but I am unable to change it. All I am getting in output is 4. Please help why is this ?
function Animal() {
this.legs…

IT Worker
- 23
- 1
- 6
1
vote
0 answers
How do declarative and object environment records relate to "var", "let", and "const"?
I am trying to dig deeper into javascript, and although I have found explanations on the differences between "var", "let", and "const" as well as explanations on ES6 "environment records" (declarative vs. object), I am still lost why the following…

Zach Gollwitzer
- 2,114
- 2
- 16
- 26
1
vote
2 answers
Edit React state with Hooks
I would like to have a global variable that I can edit anywhere using hooks.
In the example I have 2 components both using the same hook. It seems to me that the External toggle is editing its own scoped count and Internal Toggle is changing its own…

Jamie Hutber
- 26,790
- 46
- 179
- 291
0
votes
1 answer
Variable/parameter/function visibility OF a function within a function - SCOPE Question
I have been working on this for 3 hours exploring videos on scope, functions, function strings, and variable parameters. But I cannot get it right!
Quiz on Scope
Test your understanding of variable visibility.
var outside = 'outside';
function a()…

Timothy
- 1
- 2
0
votes
1 answer
How to pass baseencode image in addEventListner() function to the outer scope
I have an input field to accept image
The input field is like this
This input field is inside the template of vuejs .The vue version i am using is 3.
I am calling…

Midhun Raj
- 925
- 2
- 7
- 21
0
votes
0 answers
ES6 module constructor's scope
I've stumbled upon a behavior I cannot understand. Could someone, more proficient with JS, have a quick glance at it.
I apologize for posting screenshots instead of setting up a working example here, but I hope it should be enough for an expert to…

d.k
- 4,234
- 2
- 26
- 38
0
votes
2 answers
How can I make it so that at any point during my function the user can type "reset" and it will take them back to the beginning of my function?
let level = 0;
let usersName;
let path;
const getBotReply = (msg) => {
if (level === 0) {
level = 1;
usersName = msg;
return "Chur " + usersName + ". Do you live in Raglan?";
}
if (level === 1) {
level…

Eliot Robinson
- 1
- 1
0
votes
2 answers
JS| Initialised object is undefined after function accessing it is run more than once
I am trying to reduce the global variables in my Chrome extension to reduce the 'spaghettiness' or ambiguity in my JavaScript.
I am trying to attempt this by having an init function that declares these otherwise global variables within a mousedown…

bongoSLAP
- 384
- 1
- 3
- 11