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

Are variables declared with let or const hoisted?

I have been playing with ES6 for a while and I noticed that while variables declared with var are hoisted as expected... console.log(typeof name); // undefined var name = "John"; ...variables declared with let or const seem to have some problems…
Luboš Turek
  • 5,179
  • 6
  • 39
  • 47
108
votes
3 answers

Why does JavaScript hoist variables?

Why does JavaScript hoist variables? What was the rationale of the designers when they decided to implement hoisting? Are there any other popular languages that do this? Please provide relevant links to documentation and/or records.
Xlaudius
  • 1,417
  • 1
  • 11
  • 15
92
votes
18 answers

Javascript function scoping and hoisting

I just read a great article about JavaScript Scoping and Hoisting by Ben Cherry in which he gives the following example: var a = 1; function b() { a = 10; return; function a() {} } b(); alert(a); Using the code above, the browser…
dev.e.loper
  • 34,180
  • 71
  • 151
  • 237
85
votes
5 answers

JavaScript 'hoisting'

I came across JavaScript 'hoisting' and I didn't figure out how this snippet of code really functions: var a = 1; function b() { a = 10; return; function a() {} } b(); alert(a); I know that function declaration like ( function a() {}…
morfioce
  • 1,067
  • 2
  • 9
  • 12
67
votes
4 answers

How many JavaScript programs are executed for a single web-page in the browser?

JavaScript programs consist of statements and function declarations. When a JavaScript program is executed, these two steps occur: the code is scanned for function declarations, and every func. declaration is "executed" (by creating a function…
Šime Vidas
  • 163,768
  • 59
  • 261
  • 366
47
votes
9 answers

Which design pattern(s) take advantage of JavaScript's hoisting behavior?

Ben Cherry's excellent article explains hoisting in JavaScript adequately. My problem, however, is that I cannot conceive a use case for this notorious perpetrator of confusion. Please explain if there is a design pattern that actually takes…
David Rivers
  • 2,792
  • 1
  • 28
  • 39
41
votes
3 answers

Are ES6 module imports hoisted?

I know that in the new ES6 module syntax, the JavaScript engine will not have to evaluate the code to know about all the imports/exports, it will only parse it and “know” what to load. This sounds like hoisting. Are the ES6 modules hoisted? And if…
gilamran
  • 5,914
  • 4
  • 27
  • 48
35
votes
5 answers

Why variable hoisting after return works on some browsers, and some not?

alert(myVar1); return false; var myVar1; Above code throws error in IE, FF and Opera stating that return statement has to come in the function. But it works (shows undefined) in Safari and Chrome. The above code has been written in global scope.…
alter
  • 4,090
  • 6
  • 29
  • 35
34
votes
7 answers

'Hoisted' JavaScript Variables

I do not fully understand why the following displays "hoisted" towards the end. var x = 'set'; var y = function () { // WHAT YOU DON'T SEE -> var x; // is effectively "hoisted" to this line! if (!x) { // You might…
Grateful
  • 7,657
  • 7
  • 34
  • 59
29
votes
3 answers

Why a variable defined global is undefined?

I have here a simple function and a global variable. Why is myname undefined and not the string "global"? var myname = "global"; // global variable function func() { alert(myname); // "undefined" var myname = "local"; alert(myname); //…
J Rod
  • 528
  • 1
  • 6
  • 13
27
votes
2 answers

How does this hoisting work with block scope?

I've been asked about a question { function foo() { console.log('A'); } foo(); foo = 1; function foo() { console.log('B'); } foo = 2; console.log(foo); } console.log(foo); Why the third output is 1 instead of…
Hao Wu
  • 12,323
  • 4
  • 12
  • 39
25
votes
5 answers

What happens when JavaScript variable name and function name is the same?

I have the following code, where I declare a function and after it, a variable with the same name as the function: function a(x) { return x * 2; } var a; alert(a); I expected this to alert undefined, but if I run it, the alert will display the…
joesid
  • 583
  • 1
  • 8
  • 20
25
votes
3 answers

Will const and let make the IIFE pattern unnecessary?

As I understand it, the IIFE pattern is a work around to the fact that ES5 and below do not have a way to create block scopes. By wrapping everything in a function and immediately invoking it, we can create a scope. Now that let and const will gain…
Kyle Pennell
  • 4,306
  • 3
  • 38
  • 65
23
votes
3 answers

javascript- Uncaught SyntaxError: Identifier * has already been declared

console.log(a) //output:ƒ a(){} var a = 1; function a(){}; var a = 10; console.log(a) //output:10 ==================== var a = 1; if(true){ function a(){}; var a = 10; } console.log(a) // this code throws Uncaught SyntaxError: Identifier 'a'…
venkata
  • 320
  • 1
  • 3
  • 12
23
votes
5 answers

Need to understand Javascript function hoisting example

I read the concept of Javascript Hoisting.Its pretty confusing but I saw some examples and got the idea what hoisting actually does. So basically "Hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope…
Siddharth_Vyas
  • 9,487
  • 10
  • 36
  • 67
1
2 3
25 26