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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:24:38+00:00 2026-06-08T10:24:38+00:00

So I started learning javacript and I’ve noticed that the coding convention for functions

  • 0

So I started learning javacript and I’ve noticed that the coding convention for functions and callbacks is something like this (this is a jQuery example but I’ve seen these everywhere):

$.getJSON('some/url.json', function(a) {
// do stuff with a here
});

Coming from other languages, I would usually write the above as this:

function myFunction(myVar){
//do stuff with myVar here
};

$.getJSON('some/url.json', myFunction());

Why is the former usually the preferred way of writing in JS, instead of the [probably more readable] latter?

  • 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-08T10:24:39+00:00Added an answer on June 8, 2026 at 10:24 am

    There are plenty of good reasons to use the second, more explicit, format (assuming you fix the issue listed in the comment), including:

    • reusing the function passed as a callback
    • giving that function a definitive name to make it easier to debug
    • making sure that function is noted as something important in your system
    • separating out that functions because it’s simply too long to comfortably declare in place

    But the first format, with an anonymous function passed as a callback is short and sweet. If you don’t need to reuse the function, if it’s relatively short, and if it’s not of high importance in your system, it can be really useful to declare it exactly where you use it. You avoid adding another variable to your scope, and you make it entirely clear exactly what that function is for.

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

Sidebar

Related Questions

I've just started learning jQuery/javascript, so this might seem like a really basic question,
I started learning Jquery mobile, I followed this http://dev.jtsage.com/jQM-SimpleDialog/demos/string.html . Nothing happens when I
I have just started learning Jquery and am new to writing javascript (I am
Started learning algorithms. I understand how to find theta-notation from a 'regular recurrence' like
I just recently started learning RoR and found that Prototype.js is a default javascript
I started learning JavaScript last week, I saw this code yesterday and I did
Hey, I've just started learning JavaScript and I'm making a little script that generates
i've started learning about javascript closures, and while experimenting, i realised that the following
Ok, so I recently started learning jQuery, but I'm having trouble with a little
i have started to learning Jquery and i can't activate the resizable function. i

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.