Linked Questions
122 questions linked to/from Javascript function scoping and hoisting
89
votes
5
answers
12k
views
JavaScript 'hoisting' [duplicate]
I came across JavaScript 'hoisting' and I didn't figure out how this snippet of code really functions:
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
I know ...
29
votes
3
answers
3k
views
How the below JavaScript Scope works [duplicate]
I was reading about JavaScript scopes and Hoisting. I saw a sample as below that made some doubts on my mind. So, I was wondering how it works.
var a = 1;
function b() {
a = 10;
return;
...
4
votes
6
answers
3k
views
JavaScript fundamentals confusion [duplicate]
Hi I am trying to understand the JavaScript fundamentals, and stuck in one condition.
var foo = 1;
function bar(){
foo = 10;
return;
function foo(){}
}
...
8
votes
4
answers
3k
views
function declaration after return statement global variable is not overwritten [duplicate]
I have a Javascript code as below, http://jsfiddle.net/ramchiranjeevi/63uML/
var foo = 1;
function bar() {
foo = 10;
return;
function foo() {}
}
bar();
console.log(foo); // returns 1
...
3
votes
3
answers
1k
views
Name resolution in Javascript? [duplicate]
I was reading this article and I have some questions please:
considering this code :
1: var a = 1;
2: function b () {
3: a = 10;
4: return;
5: function a() {}
6: }
7: b();...
10
votes
1
answer
346
views
How this simple javascript script actually works (might be scoping)? [duplicate]
Can anybody break down for me into steps how this (it looks simple in the first place) is interpreted by the browser:
var a = 1;
function b() {
a = 10;
function a() {}
}
b();
alert(a);
it ...
0
votes
2
answers
2k
views
Why this global variable is undefined inside a function? [duplicate]
Why is this global variable undefined inside a function if the same global variable is re-declared and defined inside that same function?
var a = 1;
function testscope(){
console.log(a, 'inside ...
1
vote
2
answers
155
views
Why is the this code's output 242 and not 243 [duplicate]
var x = 2;
function fun() {
x = 3;
var x = 4;
document.write(x);
}
document.write(x);
fun()
document.write(x);
Can someone help me understand the flow of control. Why is the ...
1
vote
1
answer
143
views
Function call javascript [duplicate]
I called helloworld and defined it with below two different ways:
1) With variable
2) With function name itseld
var helloWorld = function() {
return '2';
}
function helloWorld() {
return '...
0
votes
1
answer
1k
views
What if we console a variable just before declare it [duplicate]
What is the difference between the following two cases:
CASE 1:
console.log(c); //console : undefined
var c;
CASE 2:
console.log(c); //console : Uncaught ReferenceError: c is not defined
Why in ...
3
votes
2
answers
133
views
Why is this javascript snippet logging 1? [duplicate]
In an interview, I was asked to guess the output of the following snippet:
var foo = 1;
function bar() {
foo = 10;
return;
function foo() {
}
}
bar();
console.log(foo);
...
0
votes
5
answers
100
views
Why does JavaScript behave this way? [duplicate]
Is the var highScore = 0 apart of the loop? Isn't scores[i] always greater than 0? I need someone to break down how the if statement is working, and I need to understand how highScore = scores[i] is ...
-2
votes
2
answers
867
views
What is the correct placement of constructors or classes in JS code? [duplicate]
Since constructors are basically an object that is stored as a copy, it seems like they are treated like a variable in the sense that they cannot be just "Anywhere" in the code, like with a function ...
0
votes
1
answer
409
views
Why there is "not a function" error if calling a function before function declaration with es6 syntax? [duplicate]
Using older syntax
Prints "this is myfunc1"
myfunc1();
function myfunc1() {
console.log("this is myfunc1");
}
Using es6 syntax
This gives error "myfunc2 is not a function"
myfunc2();
var myfunc2 =...
1
vote
2
answers
88
views
JavaScript scoping and hoisting: code returns 1 instead of 10 as expected [duplicate]
I am kind of novice in JavaScript, I really don't quite understand why the below code return 1 instead of 10:
var a = 1;
function b() {
a = 10;
return;
function a() {}
}
b();
alert(a);
...