Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

361
Views
function hoisting - why is my function not defined properly?

function goo(){
    console.log(foo)
    console.log(foo())
    {
        function foo() {return 'im foo'}
        
    }
}
goo()

I'm trying to see where the foo function definition is hoisted to when it's inside a block.

I've learnt that in non-strict mode a function definition is hoisted to the top of function scope. So I expected the foo function to be defined right after goo() is executed cuz I expect it to be hoisted to the top of the goo function.

After the hoisted definition of foo, console.log(foo) and console.log(foo()) would run and I expected it to each give me a function definition and 'im foo'.

But what ended up happening is that the console says foo is initialized to undefined and foo is not a function. I thought initalization to undefined only happened when a var variable is hoisted. When function definition is hoisted, don't they define themselves? I'm not sure why foo is defined to undefined. Console didn't raise a reference error that says foo is not declared. foo is declared alright but console says it's not a function. Why is it initalized to undefined when I wrote it to be a function that returns 'im foo' in the definition?

function goo(){
    console.log(foo)
    console.log(foo())
    function foo() {return 'im foo'}
        
    
}
goo()

I expected it to run like this code above. The only difference between the two is that foo function is defined inside a block. But why would there be a difference between the two if the foo function is hoisted to the top of goo function? Does this mean I was wrong about function definition being hoisted to top of function scope?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Both the functions foo() and goo() are hoisted during the interpretation phase, but the only catch is that foo is kept inside a block scope and hence access is not possible and you receive an error. Remember that, Block Level Scoping always affects identifier resolution(nothing to do with Hoisting). In this example, the function declarations are defined and hoisted but a function declared within a block may or may not be available outside the block.

about 4 years ago · Juan Pablo Isaza Report

0

By definition hoisting only happens for the block.

https://developer.mozilla.org/en-US/docs/Glossary/Hoisting#:~:text=JavaScript%20Hoisting%20refers%20to%20the%20process%20whereby%20the%20interpreter%20appears%20to%20move%20the%20declaration%20of%20functions%2C%20variables%20or%20classes%20to%20the%20top%20of%20their%20scope%2C%20prior%20to%20execution%20of%20the%20code.

Specifically for functions:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function#conditionally_created_functions

about 4 years ago · Juan Pablo Isaza Report

0

function goo(){
    console.log(foo)
    console.log(foo())
    {
        alert(1)
        function foo() {return 'im foo'}
        
    }
}
goo()

is equal to :

function goo(){
    console.log(foo)
    console.log(foo())
    {
        function foo() {return 'im foo'}
        alert(1)
        
    }
}
goo()

but not equal to :

function goo(){
    {
        function foo() {return 'im foo'}
        alert(1)
        
    }
    console.log(foo)
    console.log(foo())
}
goo()
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!