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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:10:17+00:00 2026-05-16T22:10:17+00:00

My understanding: in Javascript objects and arrays get passed as references not values for

  • 0

My understanding: in Javascript objects and arrays get passed as references not values for function arguments. A jQuery group is an object and hence should be passed as reference.

However I’m finding in the test script below that something strange is going on; the jQuery group is behaving like a value not a reference unless wrapped in another object … can anyone explain this?

<html>
<head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<script>

 function test(arg){
   arg = arg.add($('<span/>'))
   console.log(arg);
 };

 ele = $('<div/>');
 test(ele);  // div + span in the group as expected
 console.log(ele); // only the div - the 'arg' param in function was a copy

 function test2(arg){
   arg.a = arg.a.add($('<span/>'));
   console.log(arg.a);
 };

 obj = {a:ele};
 test2(obj); // div + span in the group as expected
 console.log(obj.a); // both in the group - arg acted like a reference!

</script>
</body>
</html>
  • 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-16T22:10:17+00:00Added an answer on May 16, 2026 at 10:10 pm

    This is a “feature” of the .add() method. It does not modify the original jQuery object, but rather returns a new object with the added value.

    Given your first example, you would need to return the arg variable and overwrite ele.

     function test(arg){
       arg = arg.add($('<span/>'))
       console.log(arg);
       return arg;  // return the new jQuery object stored in "arg"
     };
    
     ele = $('<div/>');
     ele = test(ele);  // overwrite the original "ele" with the returned value
     console.log(ele); 
    

    EDIT: To give another illustration, using your code, but with .push() which modifies the original object, you will see the correct value updated in ele.

     function test(arg){
       arg = arg.push($('<span/>')[0])
       console.log(arg);  // Because .push() is being used, "arg" will reference 
                          //   the new length of the array.
     };
    
     ele = $('<div/>');
     test(ele);  
     console.log(ele); // Will reference jQuery object with both elements
    

    EDIT: Once last illustration. Because .add() returns a new object, you could update both variables to point to the same value like this:

    ele = arg = arg.add($('<span/>'));
    

    Now instead of ele referencing the original, and arg referencing the new Object that was created, both variables hold a reference to the same Object in memory.

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

Sidebar

Related Questions

I have an object in Javascript that looks like this function MyObject(){ this.name=; this.id=0;
I thought I had a reasonable understanding of the this object in JavaScript. When
Hi I don't know whether this is my mistake in understanding Javascript prototype object
I have some javascript code (within an object) : toggle: function() { var me
I'm new to JavaScript, and am having a problem understanding this code: function addProperty(o)
Probably a lack of understanding of javascript here: engine.keyboard = {}; // keyboard object
I have a little trouble understanding scope and lifetime using javaScript and jQuery. I
I've been working on understanding Javascript objects and namespaces and I am a little
I'm having difficulty understanding variable shadowing in JavaScript based on scopes. Consider this small
I'm having trouble understanding the workings of Javascript...I want to pass a form name

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.