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