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

384 questions
17
votes
1 answer

getting around lack of "hoisting" in clojure

I have found a few times that I have a group of inter-related functions, and how I would naturally place them in the file ends up conflicting with their dependencies (i.e function 1 depends on function 2, but is above function 1). When I am writing…
Matt Briggs
  • 38,679
  • 14
  • 86
  • 125
14
votes
1 answer

Why must a module's exports be declared at the bottom of a file?

I have the following redacted code: module.exports = { read: read, write: write, }; var read = function(parameters, config, next) { /* */ }; var write = function(parameters, config, next) { /* */ }; If I go to require() this…
Scott
  • 6,386
  • 8
  • 38
  • 46
12
votes
2 answers

Order of hoisting in JavaScript

function g () { var x; function y () {}; var z; } I would like to know exactly what order the above code becomes when hoisted. Theory 1: Order between vars and functions remains as-is: function g () { var x; function y () {}; …
mareoraft
  • 2,402
  • 2
  • 19
  • 50
11
votes
3 answers

Javascript Hoisting in Chrome And Firefox

Running this in Chrome and Firefox gives different answers: (function() { if(true) { function f() { alert("yes"); }; } else { function f() { alert("no"); }; } f(); })(); In Chrome the…
BillR
  • 111
  • 4
10
votes
3 answers

Why are gcc and clang not hoisting strlen out of this loop?

Consider the following code: #include void bar(char c); void foo(const char* restrict ss) { for (int i = 0; i < strlen(ss); ++i) { bar(*ss); } } I would expect the strlen(ss) to be hoisted out of the loop in these…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
10
votes
2 answers

const variable not hoisted for immediately invoked function

I was playing around new ECMASCRIPT-6 const key word. I did not understand one particular behaviour of the keyword. Lets say I have two functions First case (function(){ console.log(_t); const _t=10; })(); and Second case function t(){ …
years_of_no_light
  • 897
  • 1
  • 9
  • 24
9
votes
1 answer

unexpected results with function and variable hoisting

I'm reading the second book of the series "You don't know JS" and I've read that functions are hoisted before variables. So this is the code: foo(); // 1 var foo; function foo() { console.log( 1 ); } foo = function() { …
BoSsYyY
  • 493
  • 3
  • 11
9
votes
2 answers

How does hoisting work if JavaScript is an interpreted language?

My understanding of an interpreter is that it executes program line by line and we can see the instant results, unlike compiled languages which convert code, then executes it. My question is, in Javascript, how does interpreter come to know that a…
Karthik Samyak
  • 282
  • 1
  • 4
  • 15
9
votes
3 answers

'use strict' not stopping hoisting in function scope

My Problem Lies here I'm learning JavaScript But not new to Programming at all. I understand hoisting, but with strict mode shouldn't this produce an error and be caught either when 6 is assigned to undeclared a variable or document.getElement... is…
Zach Hutchins
  • 93
  • 1
  • 3
8
votes
4 answers

Make sure a Javascript script is first to run?

I've noticed some scripts seem to be called before others on a certain page, I was wondering, what is the specific order for scripts to load? In-page before referenced .js scripts? Are they run in order from first