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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:26:20+00:00 2026-06-04T09:26:20+00:00

version 1: function add(){ var a = 2; … … … } version 2:

  • 0

version 1:

function add(){
var a = 2;
...
...
...
}

version 2:

$(function(){
var ...
..
..
});

Where is the main difference of two versions? For version 2, it does not have the function name. If it just simply run the code in the function, why not just remove the $function(){..};. It really makes me confusing because nowadays, many scripts are written in style of version 2. Please help to clarify my confusion.

  • 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-04T09:26:22+00:00Added an answer on June 4, 2026 at 9:26 am

    Example 1 is defining a function called add, whereas example 2 is simply passing an anonymous function to the jQuery function.

    The $(function() {...}); syntax is a shorthand for $(document).ready(function() {...});, which takes a function as its argument, and then executes that function when the DOM is ready. This is used to ensure that elements you want to work with in your Javascript actually exist before executing the code.

    Edit to address the comment below:

    The .click() jQuery function has two uses. If you pass a function then it creates an additional click event handler, which will run that function when the event is triggered. If you don’t pass a function, then it will trigger all click event handlers that have been attached to the element(s) in the jQuery object. So, yes, you can call .click() without a function, but it doesn’t do the same thing.

    You can’t do the following:

    $(document).ready(var foo = 2;...);
    

    because that will give you a syntax error. You can, however, define a function in the usual fashion, then pass that to the call to $(document).ready():

    function foo() {
         var foo = 2;
         ...
    }
    
    $(document).ready(foo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a decorated function (simplified version): class Memoize: def __init__(self, function): self.function =
I want to write a cmp -like function that compares two version numbers and
I have this jquery script: $('[id^=changesetList] tr').each(function () { var sid = $(this).attr('sid'); $(this).find('td
I have this bug Warning: date() [function.date]: It is not safe to rely on
Here is my TagAdmin class protected function configureFormFields(FormMapper $formMapper) { $formMapper ->add('name') ->add('description') ->add('slug','text',array(
I need to write a non-recursive version of the function sum-squares and Use a
I need a way to force calling a constant version of member function. I'm
Short version question : Is there navigator.mozIsLocallyAvailable equivalent function that works on all browsers,
From version to version of .NET the more function that's equal to P/Invoke is
This function is much faster than its recursive version: crossSubstrings :: String -> String

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.