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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:41:17+00:00 2026-06-14T17:41:17+00:00

I am making an app in which an algorithm is run on an array.

  • 0

I am making an app in which an algorithm is run on an array. Because the contents of the array will change during the execution of the algorithm, I’m storing the array contents to another array beforehand – performing the ‘if’ statements on the source array but updating the temporary array, then equating them afterwards.

The problem is that after running the algorithm, the two arrays are still identical. It seems that updating the temporary array automatically updates the source array.

I’ve created this jsfiddle to demonstrate:

var a = new Array( 0 , 1 , 2 );
var b = a;

b[1]=3;

document.write( (a[1]==b[1]) );

//Should show 'false' as this will not be correct

The code above returns “True”. Is this normal behaviour? How can I overcome this?

  • 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-14T17:41:18+00:00Added an answer on June 14, 2026 at 5:41 pm

    That is normal behaviour, when copying array you are actually making a reference, so when you do var b = a that means instead of copying value you are just copying the reference

    if you want to shallow copy an array (array with only one depth level) then you can use simple method like this:

    var b = a.slice(0);
    

    But if you want to deep copy an array (array with 2 or more depth level) then you can use below method for that:

    var objClone = function(srcObj) {
      var destObj = (this instanceof Array) ? [] : {};
      for (var i in srcObj)
      {
         if (srcObj[i] && typeof srcObj[i] == "object") {
             destObj[i] = objClone(srcObj[i]);
         } else {
             destObj[i] = srcObj[i];
         }
      }
    
      return destObj;
    };
    

    For use of both of these methods check this jsFiddle

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

Sidebar

Related Questions

I am Getting a weird problem. I am making an App which should run
How to change sizes of ViewController? I'm making simple app which uses deliberately two
I am making an app which will allow the user to click a picture
I'm making an app which will allow the user to purchase either a subscription
I am making an app in which user admin can change the Facebook app
I am making an android app which will retrieve some information from a remote
i m making APP which do image proceesing using java SE.i want to check
Iam making an app which has one index.php file. I also have these other
Hi I'm making an app which uses only the broadcast receiver. It serves three
Hi i'm making an app which need to get user's gmail account contact list

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.