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
19
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
  • 3,474
  • 4
  • 26
  • 62
18
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
  • 41,224
  • 16
  • 95
  • 126
14
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
  • 467
  • 2
  • 8
  • 19
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,716
  • 9
  • 40
  • 46
11
votes
1 answer

Arrow Function Hoisting in Node?

I'm having a bit of trouble understanding why my code works. I'm expecting a reference error, but everything works fine. My code: const functionA = () => { let bResult = functionB(); console.log("Function A " + bResult); }; const functionB =…
Jason
  • 412
  • 1
  • 6
  • 13
11
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
  • 118,144
  • 57
  • 340
  • 684
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
2 answers

GraphQL fields as a function

I am studying GraphQL and I get a bit confused from different implementations on the specific issue when writing the fields of a GraphQLObjectType. What is the difference between these two implementations? 1. var schema = new GraphQLSchema({ …
itaied
  • 6,827
  • 13
  • 51
  • 86
10
votes
2 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
  • 103
  • 1
  • 4
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
  • 938
  • 1
  • 10
  • 24
9
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