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

  • Home
  • SEARCH
  • 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 8424623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:02:50+00:00 2026-06-10T04:02:50+00:00

Reading A re-introduction to JavaScript I noticed something interesting about functions: The name provided

  • 0

Reading “A re-introduction to JavaScript” I noticed something interesting about functions:

The name provided to an anonymous function as above is(or at least should be) only available to the function’s own scope.

Entering some things based on the code in the tutorial at the nodejs prompt I was able to verify that node agrees with the author:

function add(foo, bar) {
  return foo + bar;
}

add(1, 2);

gets me 3, and:

var five = (function plus(foo, bar) {
             return foo + bar;
           })(2, 3);
plus(2, 3);

gets me a syntax error about plus not being defined.

I’m a little confused because the code I used to define both functions was identical (except for the name). How does JavaScript know the first one is a regular function and the second one is a named anonymous function?

  • 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-10T04:02:51+00:00Added an answer on June 10, 2026 at 4:02 am

    The first is a normal function declaration. The declared name is introduced into the current scope, and implicitly refers to the function body. In this form, function is a statement, and as such returns no value.

    The second is a function expression, and the interpreter knows that because it’s part of the right hand side of an assignment, and because of the braces around it. Any time the word function appears where some other “value” (or “expression”) could have been supplied then what you have is a function expression. The result of that expression is a reference to the function body.

    The name of a function expression (if it were given one) is only available within the function, as described in your link.

    IMHO the nomenclature in that link is incorrect. What they call a “named anonymous functions” should in my view just be called a “named function expression”. This apparent error may stem from the fact that all anonymous functions are actually function expressions, but not all function expressions are anonymous.

    Note that if you use this style:

    var foo = function bar() {
        ...
    }
    

    then as described above bar is a function expression, whose result is then assigned to the variable foo. The name bar is not put into the current scope, but is available within the function itself.

    The declaration of the variable foo will be hoisted to the top of the scope, but will not be assigned its value (i.e. the reference to the function expression) until the line above is actually executed.

    Another interesting demonstration of the difference is this (from the Chrome console):

    > function() { }
    SyntaxError: Unexpected token (
    > a = function() { }
    function () { }
    

    Note that the only difference is that in the latter there is an assignment to a. The attempt to declare an anonymous function using a statement is illegal, because the language grammar requires that the function statement include a label for the function. However the latter is implicitly a function expression, which is allowed to be anonymous.

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

Sidebar

Related Questions

Introduction I heard something about writing device drivers in Java (heard as in with
I'm reading Introduction to Algorithms book, second edition, the chapter about Medians and Order
I am reading Introduction to Algorithms and read about the insertion sort. I tried
I am reading about String algorithms in Introduction to Algorithms by Cormen etc Following
I am reading about string algorithms in Cormen's book Introduction to Algorithms. For Transition
Introduction We all know these silly arguments objects of JavaScript functions. But why object?
I am currently reading Cormen's Introduction to Algorithms and I found something called a
I am reading about DFS in Introduction to Algorithms by Cormen. Following is text
I'm reading Introduction to 3D Game Programming with DirectX 10 to learn some DirectX,
I was reading GADT introduction here and it I found the idea of restricting

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.