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

JavaScript scope conflicts

I was trying to understand the scope in JavaScript. If I declare a variable outside of a function, it is GLOBAL. Hence I tested the following code to understand sequence of execution. In the following code, I expected the "demo1" to take the global…
Sigma
  • 145
  • 1
  • 1
  • 4
7
votes
1 answer

Why is my JavaScript hoisted local variable returning undefined but the hoisted global variable is returning blank?

As part of my learning JavaScript, I try to write code to demonstrate the concept I am learning; today I'm learning hoisted variables. Here is the code I wrote: console.log("A: My name is " + name); function happy() { console.log ("1: I am "…
JimLockwood
  • 105
  • 1
  • 6
7
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
  • 4,230
  • 6
  • 30
  • 39
6
votes
2 answers

Yarn nohoist without using workspaces

One of my projects suddenly failed to compile on a Windows laptop, where the exact same code was working on a Mac. I've read about hoisting and adding nohoist, which seemed to fix the problem for Apollo client. "workspaces": { "packages": [ …
Z0q
  • 1,689
  • 3
  • 28
  • 57
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
  • 8,100
  • 5
  • 59
  • 66
6
votes
1 answer

can arrow function gets hoisted in a class? (javascript)

class App { constructor() { this.canvas = document.createElement('canvas'); document.body.appendChild(this.canvas); this.ctx = this.canvas.getContext('2d'); this.pixelRatio = window.devicePixelRatio > 1 ? 2 : 1; …
facVV
  • 379
  • 1
  • 3
  • 4
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
  • 195
  • 1
  • 2
  • 9
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
5 answers

What is the main advantage of hoisting in Javascript?

A couple of days ago, I had an interview, and one of the questions was 'what is hoisting?' then I explained the hoisting concept thoroughly, then the interviewer asked me, 'what is the main advantage of hoisting?' I couldn't answer. Really what is…
S. Hesam
  • 5,266
  • 3
  • 37
  • 59
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
  • 458
  • 3
  • 9
6
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,787
  • 3
  • 20
  • 35
6
votes
5 answers

Why doesn't hoisting exist in C#?

I use both Javascript and C# on a daily basis and I sometimes have to consider hoisting when using Javascript. However, C# doesn't seem to implement hoisting(that I know of) and I can't figure out why. Is it more of a design choice or is it more…
BenM
  • 4,218
  • 2
  • 31
  • 58
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,986
  • 4
  • 27
  • 45
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
  • 71,580
  • 16
  • 111
  • 150
1 2
3
30 31