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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:28:33+00:00 2026-06-14T04:28:33+00:00

Just out of interest, are there any speed/functionality differences between function foo(bar) { alert(foo

  • 0

Just out of interest, are there any speed/functionality differences between

function foo(bar) {
    alert("foo" + bar);
}

and

var foo = function(bar) {
    alert("foo" + bar);
};
  • 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-14T04:28:35+00:00Added an answer on June 14, 2026 at 4:28 am

    There are no significant speed differences. (Test)

    There are functionality differences.

    • Function declarations (like your first) and function expressions (like your second) are processed at different times.
    • They have different effects on the scope in which they occur.
    • Your first function has a true name, your second does not in ES5 and earlier, your second does not; in ES6/ES2015, it does, because the specification says that the JavaScript engine must assign the name of the variable to the function in that case.

    If you look around for “function declaration” vs. “function expression” you’ll find a lot of talk (some of it even correct) on the topic.

    But briefly:

    Function Declaration

    A function declaration like your first example happens when the execution cursor enters its containing scope (containing function or the global scope), before any step-by-step code is done. Therefore they cannot appear within non-function blocks (if, try, etc.), since no step-by-step code has been run when they’re processed. The name of the function is added to the scope in which it appears, and the function object has a true name (although there’s no standard way to query that name, it’s still useful in stack traces and such). (Note: Some JavaScript engines allow function declarations within blocks, but it’s invalid and what they do is not necessarily consistent. Don’t do it.)

    Anonymous Function Expression

    A function expression like your second example happens, like all expressions, when it’s encountered in the step-by-step flow of the code. Your expression is called an anonymous function expression since it doesn’t explicitly specify a name for the function. In ES5 and earlier, that meant that the resulting function had no name. In ES6/ES2015 and later, many functions created with anonymous function expressions do have names because the name can be inferred from the expression, and that’s the case with your example, in which the function ends up with the name the variable has: foo. Since anonymous function expressions are expressions, they can occur anywhere expressions can occur, although sometimes you have to warn the parser that that’s what you’re doing.

    Named Function Expression

    There’s a third way of doing this: A named function expression, rather than an anonymous one. They look like this:

    var foo = function bar() {
    };
    

    or

    var obj = {
        foo: function bar() {
        }
    };
    

    or

    doSomething(function bar() { });
    

    etc.

    They used to be really problematic cross-browser (IE8 and earlier mess them up, for instance; early versions of Safari had issues, etc.; Kangax has a good page of the problems that used to abound). It’s an expression, so it’s valid anywhere there’s an expression. The function name (bar in my example) is not added to the containing scope by a compliant JavaScript engine.

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

Sidebar

Related Questions

Just out of interest is there any major advantages/disadvantages of using either? Straight from
Just out of interest, are there any easy to setup and work with code
Is there any way (just out of curiosity because I came across multiple same-value
Out of interest, I tested if there was any difference to a for loop
Just out of interest, it would be nice to know how long my calculation
This question is just out of interest, and perhaps could be useful for my
I know this is a stupid question. But just out of curiosity, is there
I am just asking this out of interest, I did a Google search and
This question is mostly just out of academic interest. I started using YUI 3
I am working on some string manipulation functions, just out of my own interest.

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.