Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7130111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:18:55+00:00 2026-05-28T11:18:55+00:00

Possible Duplicate: Why does second function declaration win even though I return before it?

  • 0

Possible Duplicate:
Why does second function declaration win even though I return before it?

I’d like to understand one thing. Does JS first go through the code to initialize variables, functions, etc. before implementing any function or output? I mean it looks like it goes through the code twice – one time to initialize and only for the second time it begins to do something. Right?

I have such code:

alert( (function f() {
    function f() { return 1 }

    return f();

    function f() { return 2 }
})() );

The output is 2. So if it begins to implement the code right from the first time, the output would be 1. But as far as output is 2 it first checks the code and only then begins to work with it. Am I right?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T11:18:55+00:00Added an answer on May 28, 2026 at 11:18 am

    Javascript does “hoisting”. This basically means that any var name or function name() is hoisted to the top of whatever function body it’s declared in. So your example could be re written like this, which obeys hoisting rules.

    var f;
    f = function() {
      var f;
      f = function() { return 1; };
      f = function() { return 2; }; // hoisted!
      return f();
    };
    alert(f());
    

    What’s happening here is that in the outer scope, you make a local variable f. You then assign a function to it, and then execute that function.

    Inside the function, we create a new local variable f, which shadows the outer one, making it inaccessible. We now have a totally different f that only exists in this inner function. We assign the first function to it, and then the second hoisted function right after. Now we execute our function body.

    As a result, you almost never want to use the function name() {} syntax for creating functions because of hoisting. Instead, use var name = function() {} to create functions.

    It’s far more controllable and understandable because declaring the variable and assigning a value to it are now 2 separate operations. The variable declaration is still hoisted, but it won’t have a value until it reaches the line of code that assigns a function to it, which is usually what you actually want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like
Possible Duplicate: What is the difference between a function expression vs declaration in JavaScript?
Possible Duplicate: Does every thread need its own autorelease pool? I would like to
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: What does map(&:name) mean in Ruby? I was watching a railscast and
Possible Duplicate: How does an underscore in front of a variable in a cocoa
Possible Duplicate: What does a type followed by _t (underscore-t) represent? While typing in
Possible Duplicate: What does ||= (or equals) mean in Ruby? It's hard to search
Possible Duplicate: How does StackOverflow generate its SEO-friendly URLs? I made a simple form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.