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
-1
votes
1 answer

Javascript function called too soon

I'm having a problem with a Javascript function that apparently gets called too soon. I have a hunch this is a hoisting problem, but I'm not sure. So, I have this function which is assigned to onclick of an : function…
Nicola
  • 379
  • 3
  • 14
-1
votes
1 answer

Javascript hoisting confusion

I read Kyle's I don't know JS and came to know that function declarations will hoist first before the var. So in the below code