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
6
votes
2 answers

Understanding JavaScript hoisting and truthy & falsy

I've been reading about JavaScript hoisting sometime back. JavaScript Scoping and Hoisting by Ben Cherry Two words about “hoisting” by Dmitry Soshnikov and, some more about JavaScript type-coercion, truth & false test: Truth, Equality and…
manikanta
  • 7,066
  • 5
  • 52
  • 61
6
votes
2 answers

How does JavaScript's lexical environment maintain variables declarations within nested block scopes?

I've read a couple of more comprehensive articles on the execution context and now I am sort of confused and messed up in the head. To keep the question as brief as possible avoiding long citations I better try to illustrate my mental model through…
fekaloid
  • 173
  • 1
  • 1
  • 6
6
votes
1 answer

javascript function Hoisting inside block statement

{function foo(){};foo=1;function foo(){};foo=2;} console.log(foo); // 1 Can anyone explain why "1" is output here? Edit: Seems there is an implementation difference, within "Chrome", "Firefox", "Nodejs" the output is "1", but within "Safari"…
Andre Geng
  • 61
  • 3
6
votes
2 answers

a variable and function with same name returns an error inside a block

If we declare a variable and a function with same name, it is accepting re-declaration. But when we do the same thing inside a block, it shows re-declaration error. Code: var x; function x() {}; // no error. But in this case i'm getting…
6
votes
1 answer

Variable hoisting inside IIFE (lazy parsing)

I am getting a very strange output on the below scenarios: function test(){ var test=123; console.log(test) } // this output : 123 (function test(){ var test=123; console.log(test) })() // this output: 123 But when using the below…
ankur kushwaha
  • 418
  • 3
  • 8
6
votes
1 answer

Will scala compiler hoist regular expressions

I wonder if this: object Foo { val regex = "some complex regex".r def foo() { // use regex } } and this: object Foo { def foo() { val regex = "some complex regex".r // use regex } } will have any performance difference. i.e.,…
lyomi
  • 3,820
  • 5
  • 25
  • 38
6
votes
2 answers

which is the better way of defining a function?

Is there any difference between them ? I've been using both the ways but do not know which one does what and which is better? function abc(){ // Code comes here. } abc = function (){ // Code comes here. } Is there any difference between…
Janak
  • 4,630
  • 4
  • 24
  • 42
6
votes
1 answer

Why do catch clauses have their own lexical environment?

Consider the following excerpt from ECMA-262 v5.1 (which I recently saw in this question): A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical…
bfavaretto
  • 69,385
  • 15
  • 102
  • 145
5
votes
5 answers

Trying to fully understand JavaScript hoisting

Edit Looks like it was an issue on my part and my usage of jsfiddle :? Ive been reading a couple of articles on hoisting lately, one is by Nicholas Zakas, and the other is by Ben Cherry. Im trying to follow the examples and just test on my own to…
Loktar
  • 32,547
  • 8
  • 83
  • 101
5
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
  • 213
  • 1
  • 8
5
votes
1 answer

Scoping and closure oddities in javascript

This was presented yesterday at TC39. You can find the gist here: var p = () => console.log(f); { p(); // undefined console.log(f); // function f(){} f = 1; p(); // undefined console.log(f); // 1 function f(){} p(); // 1 …
kstratis
  • 6,429
  • 9
  • 39
  • 80
5
votes
2 answers

function definitions not hoisted

W.r.t Hoisting of fxn definitions. if (true) { function foo() { alert(1) } } else { function foo() { alert(2) } } foo() Chrome, some 2-3 months ago - would print 2. Now, it's printing 1. Did I miss something or, did…
Vivek Chandra
  • 4,016
  • 8
  • 27
  • 37
5
votes
1 answer

Debugger Engine. Method rewriting, local variables hoisting and variables resolution

I'm making a managed .NET debugger using MDBG sample. It works for straightforward scenarios, but has issues when method rewriting occurs. Most critical parts are yield method and async methods. I've already asked a more general question about these…
3615
  • 3,487
  • 3
  • 14
  • 31
5
votes
3 answers

If I want to "use" hoisting, is there a downside to using function expressions instead of regular function declarations?

I'm learning JavaScript, and I feel like I understand hoisting decently enough, so I'm not asking what it is or how to do it or anything like that. Is it good to hoist? If I can, should I be declaring my variables using var foo = function() {}; Or…
Senichi
  • 53
  • 3
5
votes
3 answers

javascript variable scope in function confusion

Here are 2 javascript functions var a = 10; function abcd() { alert(a);//alerts 10 a=5; } And another code is this var a = 10; function abcd() { alert(a);//alerts undefined var a=5; } In both function assignment/declaration is after alert()…
Rohit Awasthi
  • 686
  • 3
  • 11
1 2
3
25 26