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
-4
votes
7 answers

What is difference between hoisting var x = 3 and var x

Consider the two following snippets function x(){} var x = 3; console.log(typeof x) //"number" This happens because function declarations are hoisted first. function x(){} var x; console.log(typeof x) //"function" I expect the type to be…
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
-7
votes
3 answers

Value after hoisting?

Question: What will be the value of bar after it is hoisted? var bar = function() { return 3; }; I think: the function expression. I'm wrong?
-9
votes
2 answers

Why is my JavaScript not hoisting functions?

Me and a few friends are making a game in JavaScript, and it's getting really long, it is currently at 2942 lines long. A while ago I noticed that JavaScript wasn't properly hoisting my functions. I have tested this in Firefox, Chrome, and Safari…
1 2 3
30
31