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 8078983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:57:57+00:00 2026-06-05T15:57:57+00:00

Possible Duplicate: What is the difference between a function expression vs declaration in Javascript?

  • 0

Possible Duplicate:
What is the difference between a function expression vs declaration in Javascript?

Is there a MAJOR difference between declaring functions in these ways:

  1. function foo(){
         alert('BAR');
    }
    
  2. var foo = function (){
         alert('BAR');
    }
    
  3. var foo = function bar(){
         alert('BAR');
    }
    

I was told here that:

It happens at a different time, and results in a variable referring to an anonymous function. A function declaration happens prior to any stepwise code executing in the scope, and results in both a binding and a function with a proper name.

Can the way I declare my function really affect the efficiency of my code, and if so which way is best to use?

  • 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-06-05T15:58:01+00:00Added an answer on June 5, 2026 at 3:58 pm

    Yes, there is a major difference.

    The first is a function declaration. It happens upon entry to an execution context, prior to any step-by-step code being processed. It cannot be within any kind of control block (e.g., it’s not legal in the body of an if statement; however, most browsers will try to accommodate it if you do that — sometimes resulting in very surprising behavior — at variance with the spec). It results in a named function.

    The second is a function expression (specifically, an anonymous function expression). Like all expressions, it’s processed when it’s encountered in the step-by-step execution of the code. And like all expressions, it can be within a control block. It results in a function with no name assigned to a variable that has a name.

    The third is a named function expression. It’s a function expression like the above, but the function is also given a name. You want to avoid these with IE8 and earlier, since IE will actually get it quite wrong, creating two separate functions (at two different times). (Basically, IE treats it as both a function declaration and a function expression.) IE9 finally gets this right.

    Note that your second and third examples rely on automatic semicolon insertion; because those are both assignment statements, they should end with a ; (after the ending } of the function).

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

Sidebar

Related Questions

Possible Duplicate: What is the difference between a function expression vs declaration in JavaScript?
Possible Duplicate: What is the difference between a function expression vs declaration in Javascript?
Possible Duplicates: What is the difference between a function expression vs declaration in JavaScript?
Possible Duplicate: what is the function __construct used for? is there any difference between
Possible Duplicate: Is there a difference between (function() {…}()); and (function() {…})();? I've seen
Possible Duplicate: Functions vs methods in Scala What is the difference between def foo
Possible Duplicate: Location of parenthesis for auto-executing anonymous JavaScript functions? Is there a difference
Possible Duplicate: The difference between the two functions? (“function x” vs “var x =
Possible Duplicate: What is the difference between a ‘function’ and a ‘procedure’? I searched
Possible Duplicate: JavaScript: Class.method vs. Class.prototype.method What's the difference between creating a prototype like

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.