Questions tagged [hoisting]

Hoisting means that local variables inside the scope of a function are treated as if they are declared at the start of the function's evaluation regardless of where they actually are declared.

Hoisting enables variables and functions to be declared in any order. Hoisting variable declarations outside of loops is a common compiler optimization. For example:

function g(A, x, y)
  for k = 1:2000
    x = x[k]
    for l = 1:2000
        if x && y[l]
          A[l,k] += .1
        end
    end
  end
end

function g(A, x, y)
  k = 0
  x = x[k]
  for k = 1:2000
    for l = 1:2000
        if x && y[l]
          A[l,k] += .1
        end
    end
  end
end

Function declarations are hoisted in the following languages:

  • JavaScript
  • ActionScript
  • VBScript
  • Python

Variable declarations are hoisted in the following languages:

  • JavaScript
  • ActionScript
  • VBScript

References

453 questions
5
votes
5 answers

Trying to fully understand JavaScript hoisting

Edit Looks like it was an issue on my part and my usage of jsfiddle :? Ive been reading a couple of articles on hoisting lately, one is by Nicholas Zakas, and the other is by Ben Cherry. Im trying to follow the examples and just test on my own to…
Loktar
  • 34,764
  • 7
  • 90
  • 104
5
votes
1 answer

Confusion about Javascript Hoisting

I thought I grasped the concept of hoisting, but the code below got me confused. How does it return 1? Will the second example() function be hoisted above the first one? function example() { return 9; } console.log(example()); function…
Roma Kim
  • 321
  • 1
  • 8
5
votes
2 answers

Are function parameters in JavaScript hoisted?

function foo(a,b){ return a + b; } foo(1,2); Are function parameters hoisted? Does the variableEnvirnoment at the creation phase of the function execution context looks something like that : VE = { { 0 : undefined , 1: undefined,…
Abdou Bestmood
  • 475
  • 4
  • 15
5
votes
3 answers

JavaScript: The result of undefined++ is NaN, not undefined?

The 1st console.log output is 2. No doubt. But why the 2nd console.log output is not undefined? Shouldn't output the undefined at first, then the variable b becomes NaN? var a = 2; console.log(a++); var b; console.log(b++);
Danson Lin
  • 51
  • 3
5
votes
2 answers

javascript hoisting: what would be hoisted first — variable or function?

Recently I was confused about javascript hoisting behavior and now I got stuck with that. So, there are two examples. var alpha = 'alpha'; var beta = 'beta'; f(); //beta var f = function f1() { console.log(beta); }; function…
Avernikoz
  • 463
  • 2
  • 5
  • 15
5
votes
1 answer

Scoping and closure oddities in javascript

This was presented yesterday at TC39. You can find the gist here: var p = () => console.log(f); { p(); // undefined console.log(f); // function f(){} f = 1; p(); // undefined console.log(f); // 1 function f(){} p(); // 1 …
stratis
  • 7,750
  • 13
  • 53
  • 94
5
votes
2 answers

function definitions not hoisted

W.r.t Hoisting of fxn definitions. if (true) { function foo() { alert(1) } } else { function foo() { alert(2) } } foo() Chrome, some 2-3 months ago - would print 2. Now, it's printing 1. Did I miss something or, did…
Vivek Chandra
  • 4,240
  • 9
  • 33
  • 38
5
votes
3 answers

If I want to "use" hoisting, is there a downside to using function expressions instead of regular function declarations?

I'm learning JavaScript, and I feel like I understand hoisting decently enough, so I'm not asking what it is or how to do it or anything like that. Is it good to hoist? If I can, should I be declaring my variables using var foo = function() {}; Or…
Senichi
  • 53
  • 3
5
votes
2 answers

Javascript - Precedence in hoisting

In hoisting, do variables take precedence over function definition or the other way round? Please see the code below: function a() { var x = 10; function x() { return 20; } return x; }
Nikhil Pai
  • 127
  • 1
  • 7
5
votes
2 answers

javascript hoisting for global variable

I was wondering how javascript hoisting works for global variable. Let's say I have following code snippet: var a = 5; function print(){ console.warn("a",a,b); var a = 10; b=5; console.warn("a",a); } print(); In this case I am…
Mayank
  • 112
  • 8
5
votes
4 answers

Hoisting of JS variables declared without 'var'

I am trying to get my head around hoisting and scopes in JavaScript, and am trying to figure out what exactly happens in this block of code. console.log(outside) and console.log(local) both log undefined, as I expected, as outside is declared but…
a3onstorm
  • 406
  • 5
  • 13
5
votes
3 answers

javascript variable scope in function confusion

Here are 2 javascript functions var a = 10; function abcd() { alert(a);//alerts 10 a=5; } And another code is this var a = 10; function abcd() { alert(a);//alerts undefined var a=5; } In both function assignment/declaration is after alert()…
Rohit Awasthi
  • 686
  • 3
  • 12
5
votes
2 answers

WHY JSLint complains: "someFunction() was used before it was defined"?

Searching for the JSLint error "was used before it was defined" i've found these: JSLint: Using a function before it's defined error Function was used before it was defined - JSLint JSLint: was used before it was defined jsLint error:…
7hi4g0
  • 3,729
  • 4
  • 22
  • 32
5
votes
3 answers

JavaScript scope gets changed? Why does this happen?

The value of 'a' seems to lose global scope when the constructor a is called. var a = 6; function b() { a = 10; function a() {} console.log(a); //10 } b(); console.log(a); //6
user2430508
  • 1,011
  • 1
  • 7
  • 6
5
votes
2 answers

Hoisting in javascript

I asked a question before and somebody give me a guide and I read it and I saw this var temp = setTimeout, setTimeout = function() {}; He said that temp will be undefined due to JavaScript hoisting and I dont understand why Its not should be…
user1801625
  • 1,153
  • 3
  • 13
  • 14