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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:15:23+00:00 2026-05-17T00:15:23+00:00

I know that there are several ways to define a function in JavaScript. Two

  • 0

I know that there are several ways to define a function in JavaScript. Two of the most common ones are:

(1)  function add (a, b) {
         return a + b;
     }

(2)  var add = function (a, b) {
         return a + b;
     }

I am comfortable with the idea of a function as an object that can be passed around just like any other variable. So I understand perfectly what (2) is doing. It’s creating a function and assigning to add (let’s say this is in the global scope, so add is a global variable) the said function. But then what is happening if I use (1) instead? I already know that it makes a difference in execution order: if I use (1) then I can refer to add() before the point in the code where add() is defined, but if I use (2) then I have to assign my function to add before I can start referring to add().

Is (1) just a shortcut for (2), albeit one that happens to behave like other C-style languages in allowing us to define a function “below” the point at which it’s used? Or is it internally a different type of function? Which is more “in the spirit” of JavaScript (if that isn’t too vague a term)? Would you restrict yourself to one or the other, and if so which one?

  • 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-17T00:15:23+00:00Added an answer on May 17, 2026 at 12:15 am

    It looks like you are already aware of the main characteristics of function declarations1 (1) and function expressions (2). Also note that in (1) there is still a local variable called add containing a function value, just like in (2):

    function hello () {
       alert('Hello World');
    }
    
    console.log(typeof hello);  // prints "function"
    
    setTimeout(hello, 1000);    // you can still pass functions around as arguments,
                                // even when using function declarations.
    

    One other point worth mentioning is that function declarations (1) shouldn’t be used to define functions conditionally (such as in if statements), because as you have mentioned, they are automatically moved to the top of the containing scope by the JavaScript interpreter2. This is normally referred to as hoisting.

    As for which approach is more in the spirit of JavaScript, I prefer using function expressions (2). For a more authoritative opinion, Douglas Crockford lists function declarations (1) in the “Bad Parts” chapter in his popular The Good Parts book2.


    1 Also known as function statements (See @Tim Down’s comments below).
    2 Actually some browsers are able to handle function declarations in if statements (Again refer to comments below).
    3 JavaScript: The Good Parts – Appendix B: Page 113.

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

Sidebar

Related Questions

I know that there are several ways of achieving this solution so I was
I know there are several ways of creating a free trials. My algorithm that
I know that there is no return type of the constructors in C++ However,
One of the things about programming is that there are several ways to achieve
I have read it on the official website that there are several ways in
It came to my attention that there a several ways to compare strings in
I know there are several ways to do it, but I would like to
I know that a structure can be defined by in several ways such as:
As seen in this other answer , there are several ways to iterate two
I know that there are some threads have a similar issue with this thread.

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.