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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:48:03+00:00 2026-06-10T01:48:03+00:00

I’m learning javascript and I’m trying to figure out why my below function doesn’t

  • 0

I’m learning javascript and I’m trying to figure out why my below function doesn’t work as expected. See the explanation in the 2 code examples:

// First time I call the shoplist function I pass [1] in the argument. Results are as I expect:

var shopitems = [];

function shoplist(ids) {
    alert("ids passed to shoplist function: " + ids); // 1
    alert("current ids in shopitems var: " + shopitems); // (empty)
    shopitems.push(ids);
    alert("ids in shopitems after pushing: " + shopitems); // 1
    }


// Second time I call the shoplist functions I pass [1, 2] in the argument. Results are not what I would expect:

function shoplist(ids) {
    alert("ids passed to shoplist function: " + ids); // 1, 2
    alert("current ids in shopitems var: " + shopitems); // 1, 2  <--- Why is there 1, 2 and not only 1?
    shopitems.push(ids);
    alert("ids in shopitems after pushing: " + shopitems); // 1, 2, 1, 2

EDIT: Here is the full code (warning: probably very confusing): http://dpaste.org/wXMy5/

  • 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-10T01:48:05+00:00Added an answer on June 10, 2026 at 1:48 am

    Your problem will occur if you are modifying the SAME ids array before calling the second invocation of shoplist(). Because javascript passes arrays by reference and only a reference goes into the shopitems array, when you modify the ids array before passing it to the second invocation of shoplist(), you are also inadvertently modifying shopitems[0] too. If each of your arguments to shoplist() the first and second time you call it are completely separate arrays, you will not have this problem, but if the second invocation is just being passed a modification of the first array, you will have this problem.

    The quick illustration is this:

    // this will not have the problem because each call to shoplist
    // is passing a completely separate array
    var list = [1];
    shoplist(list);
    list = [1,2];      // create new array
    shoplist(list);    // shoplist is [[1], [1,2]]
    
    // this will have the problem because they are the same array
    var list = [1];
    shoplist(list);
    list.push(2);     // modify first array
    shoplist(list);   // shoplist is [[1,2], [1,2]] and both array elements are actually the same array
    

    For more detailed explanation: .push(ids) adds a whatever the contents of ids as a new items onto the end of the shopitems array. So, each time you call shoplist, you get a new item on the end of shopitems. But, since the item you are adding is an array, it adds a reference to that array, not a copy of that array. If you subsequently change that array, the shopitems array entry will point to the changed version of the array.

    You can see that in this code:

    var x = [];
    var list = [];
    x.push(1);       // contains contains [1]
    list.push(x);    // list is [[1]]
    x.push(2);       // x is [1,2]
    list.push(x);    // list is [[1,2], [1,2]]  (contains two references to x)
    

    In this code example, list will contain two elements and each will point to the same live version of x which contains [1,2].

    This is because by default, javascript passes references for things like arrays and objects. When you push an array element into your container array, it is not putting a static copy of that variable into the array. It’s putting a pointer to the original variable. If you then change the original variable, that change is reflected in the array too.

    To separate the second entry from the first, you either need to consciously make a copy of the first array and push that copy into the container array or you need to create a new array from scratch and push it into the container array.

    For example, here are a couple ways to create two independent elements in the container array:

    var x = [];
    var list = [];
    x.push(1);       // contains contains [1]
    list.push(x);    // list is [[1]]
    x = [];          // set x to a new array (the old version of x is still in list)
    x.push(1);       // x is [1]
    x.push(2);       // x is [1,2]
    list.push(x);    // list is [[1], [1,2]]  (contains two separate items)
    

    Or, make a copy of x:

    var x = [];
    var list = [];
    x.push(1);       // contains contains [1]
    list.push(x);    // list is [[1]]
    x = x.slice(0);  // make a copy of x, the old version of x is still in list
    x.push(1);       // x is [1]
    x.push(2);       // x is [1,2]
    list.push(x);    // list is [[1], [1,2]]  (contains two separate items)
    

    The important thing to remember here is that in javascript, object assignment or array assignment does not make a copy. It just assigns a pointer to the original data structure. If you change the original data structure, that will be reflected in any assignments you’ve made.

    If you a copy, you have to either explicitly make a new array or explicitly make a copy.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.