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

Confused about hoisting

consider these slightly two different versions of hoisting... mylocation = "dublin" function outputPosition() { alert(mylocation); mylocation = "fingal" ; alert(mylocation); } outputPosition(); This will output "fingal" and then…
dublintech
  • 16,815
  • 29
  • 84
  • 115
4
votes
2 answers

Why does cypress variable inside of it() stay as the last value assigned to that variable, whereas outside of it() it works correctly? (Example)

If my code is: context('The Test', () => { let testStr = 'Test A' it(`This test is ${testStr}`, () => { expect(testStr).to.eq('Test A') }) testStr = 'Test B' it(`This test is ${testStr}`, () => { expect(testStr).to.eq('Test B') …
4
votes
3 answers

Does automatically hoisting slow down the performance of JavaScript?

Lately, I was studying Scope in Javascript. I want to know whether automatically hoisting is done at compile time or at the time of executing the code(run time). If it does at run time then I have another question does auto-hoisting will slow down…
Subrato Pattanaik
  • 5,331
  • 6
  • 21
  • 52
4
votes
2 answers

Function Expression itself cannot assign Name to another Value

In the code below: (function (){ function test(){};//"function" var test;//"undefined" var printTest = typeof test; document.write(printTest); })(); printTest will display "function" instead of "undefined", which makes sense since…
Benny Tjia
  • 4,853
  • 10
  • 39
  • 48
4
votes
1 answer

Vue.js - using functions in component methods

I have a vue2 component, where some methods are pretty long with a lot of callbacks and I would like to structurize it better. I try to follow the guidelines as per callbackhell.com but things don't look that easy within vue method. Below is the…
Jan Zikmund
  • 646
  • 9
  • 19
4
votes
2 answers

Hoisting of javascript with block statement

foo(); var a = true; if (a) { function foo() { console.log( "a" ); } } else { function foo() { console.log( "b" ); } } My expected output is b but when I try to run in browser got foo is not a function
4
votes
1 answer

Are ES6 blocks only prevent function hoisting?

Someone please help me understand the following scenario: //Outer funtion function foo() { console.log('outer foo'); } { //Inner function function foo() { console.log('inner foo'); } } foo(); //Says "inner foo" I…
Charlie
  • 22,886
  • 11
  • 59
  • 90
4
votes
3 answers

Double let declaration: How does let hoisting work in for loops?

Given the following simplified code, why do I get a ReferenceError? function foo(n){ for (let i = 0; i < n; i++) { console.log(i); let i = 10; } } foo(4); // OUTPUT: ReferenceError: i is…
leonardofed
  • 936
  • 2
  • 9
  • 24
4
votes
3 answers

Property added to a JS object as a prototype is hoisted, whereas a prototype function is not

I am (or at least I thought I was) pretty much familiar with the concept of Hoisting in JavaScript. Consider the following statements: A function declaration will be hoisted along with its body, whereas a function expression will not; only the var…
iamdanchiv
  • 4,052
  • 4
  • 37
  • 42
4
votes
3 answers

Why is no ReferenceError being thrown if a variable is used before it’s declared?

I’m trying to wrap my head around the behavior of reference errors thrown in JavaScript. In the following example, a ReferenceError is thrown at the second line, and execution breaks: var obj = {}; obj.func1 = func2; alert('Completed'); Whereas in…
4
votes
2 answers

How does this Angular JS code snippet work?

Why does following snippet work in Angular JS? var app = angular.module('store',[]); (function(){ app.controller('StoreController',function(){ this.blabla = student; }); })(); var student = { name:"Abc Def", …
user4904589
  • 547
  • 2
  • 5
  • 15
4
votes
1 answer

Are there any instances in which hoisting behavior is beneficial

I came across the topic of Hoisting while having some casual discussion with JavaScript developers, and I am told that Hoisting is NOT a mistake, error or poorly written functionality but a powerful tool for developers. Can anyone explain how…
krishna
  • 575
  • 2
  • 8
  • 22
4
votes
3 answers

Function names defined as parameters to a function call aren't hoisted. Why not?

Consider the following code. Notice that a is seemingly accessed before it is defined. The console output is: (jsfiddle) function a() {} Function and variable names are defined…
Chris Calo
  • 7,518
  • 7
  • 48
  • 64
4
votes
1 answer

Why is my variable not being hoisted?

I have a global variable i which I increment (see fiddle here): (function increment() { i += 1; })(); i = 0; ​In Chrome, I get the error Uncaught ReferenceError: i is not defined. Shouldn't the variable i be hosted here, so that inside the…
Randomblue
  • 112,777
  • 145
  • 353
  • 547
3
votes
2 answers

Should JavaScript minifiers automatically combine var declarations?

I just discovered that YUICompressor (2.4.7) does not combine var declarations. For example, var x = 1; var y = 2; compresses to var a=1;var b=2; I assume it would be reasonable to expect a minifier to be able to combine consecutive var…
kojiro
  • 74,557
  • 19
  • 143
  • 201