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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:40:59+00:00 2026-05-24T19:40:59+00:00

I have come across a strange bug in my code and I cannot understand

  • 0

I have come across a strange bug in my code and I cannot understand why it happens.

I have an array array1. I duplicate array1 by making array2 equal to array1. I then modify array2 using splice to add a number. Array1 should not be touched? But both output the same change.

var array1 = [0,1,2,3,4,5];
var array2 = array1;
array2.splice(1,0,1) //add  
console.log(array1);
console.log(array2);

I am assuming I am confusing array assignment? What is the proper way to duplicate arrays without this happening?

Cheers

  • 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-24T19:40:59+00:00Added an answer on May 24, 2026 at 7:40 pm

    Use array1.concat() to duplicate the array instead of passing a reference to array1:

    var array1 = [0,1,2,3,4,5];
    var array2 = array1.concat();
    array2.splice(1,0,1) //add  
    console.log(array1);
    console.log(array2);
    

    array.concat() can concatenate multiple arrays, but if you pass an empty argument, you’re effectively concatenating an array with nothing: cloning the array.

    Note that any array and object elements are still references:

    var a = [ [1], 2];
    var b = a.concat();
    b[0][0] = 0;
    console.log(b); // gives 0,2
    console.log(c); // gives 0,2 too!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across some strange behaviour, and I'm assuming a bug in Firefox,
I have come across a strange behavior of Java that seems like a bug.
I've come across a strange render bug on iPhone OS 3.0... I have two
I have come across a strange behavior with Microsoft Code Contracts that I don't
I have just come across something that is quite strange and yet I haven't
I have come across the following type of code many a times, and I
We have come across a strange issue with ConcurrentHashMap , where two threads appears
I've come across a really strange problem. I have written a simple Deck class
I have come across a strange defect. I am using Orbeon 3.9 CE on
I have come across a strange problem with Zend_Framework, I can not load forms

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.