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
3
votes
1 answer

why when using console.log() on a variable created afterwards but in another script tag i get an error instead of logging 'undefined' in the console?

why would this code snippet give an error in the console "Uncaught ReferenceError: x is not defined" while this one logs…
3
votes
2 answers

Const is not defined in global scope but defined in local?

Why const is not defined in global scope but defined in local? screenshoot from devTools { console.log(b); const b = 2; } VM366:2 Uncaught ReferenceError: Cannot access 'b' before initialization at :2:17 console.log(a); const…
3
votes
1 answer

YDNJS: scope and closures hoisting wrong example

I am reading YDNJS: scope and closures, And in chapter 4 which talks about hoisting it says that Function declarations that appear inside of normal blocks typically hoist to the enclosing scope, rather than being conditional as this code…
3
votes
1 answer

Why can't assignment be done before the variable declaration with let and const?

I've read on multiple websites (like w3schools) that hoisting is "the behavior of moving all declarations to the top of the current scope". For let and const, the variables are hoisted but not initialized. I understand why the following code does…
3
votes
0 answers

Hoisting clarification

Javascript engine executes in two phases: creation phase and execution phase. During the creation phase, variables are allocated memory. Here, allocated memory means that var variables are declared and initialized with undefined whereas let and…
kJo
  • 55
  • 1
  • 5
3
votes
1 answer

What are the benefits and drawbacks of variable hoisting in programming languages?

The following Python program A outputs 1, as expected, while the following Python program B raises an unbound local variable x error, counterintuitively. Program A: def f(): print(x) x = 1 f() Program B: def f(): print(x); x = 2 x =…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
3
votes
2 answers

Is there any benefit from hoisting?

I would like to see which are the benefits of hoisting, if there's some... I looked for an answer but they only explain what is it, I would like to know is there's an actual benefit that I can use to write better code. It seems that with the use of…
jean182
  • 3,213
  • 2
  • 16
  • 27
3
votes
4 answers

How JS hoisting works within functions?

Can someone explain to me why (1) returns 11 where (2) returns undefined. what is the effect of function blocks/declarations to hoisting ? // (1) var boo = 11 console.log(boo) // 11 var boo = 10 // (2) var boo = 11 function foo() { …
Fahd Lihidheb
  • 671
  • 4
  • 20
3
votes
1 answer

ReferenceError: can't access lexical declaration`X' before initialization

Can someone explain me the weird error messages that occur both in chrome and firefox when trying to access a variable before the let declaration: "let X". if we write something like: console.log(X); let X; /* In firefox it reports an error…
Kevin
  • 209
  • 2
  • 11
3
votes
1 answer

Variable 'name' doesn't return undefined

I'm learning about hoisting in JavaScript. When I try this code console.log('name', name) console.log('age', age) console.log('occupation', occupation) var name = 'tom' var age = '23' var occupation = 'builder' in my developer tools in chrome i…
Diotr
  • 65
  • 6
3
votes
3 answers

Why is variable definition not hoisted in JS but function

I am learning JS and i am curious about why a function can be called before its defined & yet run fine. I understand that its due to hoisting wherein the code is scanned upfront to bring in declared variables & functions in scope. If it were just…
nikel
  • 3,402
  • 11
  • 45
  • 71
3
votes
1 answer

How to explain the following examples? Hoisting?

I tried to explain the problem with the javascript hoisting, but I couldn't explain the case of b. Didn't b = 50 modify the global variable b? Is it the reason for block-level scopes? Environment Chrome 77 { a = 50 function a()…
Annani Zhu
  • 33
  • 4
3
votes
1 answer

Which version of ECMA specification introduced hoisting?

Does anyone know when the specifications for hoisting was introduced? And whether internet explorer version 6 supported it? I didn't realize hoisting existed because these days, all my JavaScript code is transpiled. I vaguely remember feeling…
John
  • 32,403
  • 80
  • 251
  • 422
3
votes
2 answers

function hoisting shows different results with and without block scope

This is the example: function b() { console.log(f); { function f() {} } } b() I thought it would become: function b() { // hoist to function scope function f() {} console.log(f); // should output function…
3
votes
1 answer

local undeclared variables becoming global. Where in the document do they end up?

I'm lacking understanding in what happens to an undeclared variable and where it ends up in the document. For example var a = 1; function b() { a = 10; return; function a() {} } b(); console.log(a); with hoisting would become function b () { …