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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:19:36+00:00 2026-06-14T15:19:36+00:00

I think this is just me not understanding part of how JavaScript works. Let’s

  • 0

I think this is just me not understanding part of how JavaScript works. Let’s say I have an array, call it arr1, with 6 ints in it, [1,2,3,4,5,6]. If I make a new array:

var arr2 = arr1

(for the purpose of maintaining an unchanged copy of arr1), when I change arr1 the changes are reflected in arr2.

Basically, I am manipulating arr1. For testing purposes I wanted to have an unchanged copy of arr1 so that when I’m done I can console.log them both out on my web page and see the differences in them. But again, when I make changes in arr1, that change is reflected in arr2.

Can anyone explain why this happens and maybe a way around it? I’m more interested in why this happens than how to fix it.

One approach is to make arr2 a separate array, and use a for loop to populate it with arr1‘s data

for(int i = 0; i < arr1.length; i++) arr2[i] = arr1[i]

but, if the array was huge, that might be expensive. Any help is appreciated.

  • 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-14T15:19:38+00:00Added an answer on June 14, 2026 at 3:19 pm

    When you assign non-primitives (such as arrays), you’re not making a copy. You’re creating another reference to the same array.

    If you want to copy an array:

    var arr2 = arr1.slice();
    // or
    var arr2 = arr1.concat();
    

    More reading: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array

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

Sidebar

Related Questions

I have tried every way I can think of with this, but just not
I think this is just a Best Practices question, but I was wondering if
[see later answer for more] I think this is just a simple rails question,
I don't think that this could be done in C#, but posting this just
Best explained with code I think, this is just a simple example: public class
I've been thinking about this and I just can't think of a way to
Rookie question, I think. I'm just trying to replicate this: http://rpubs.com/gallery/googleVis Open a new
I think this is more of my lack of understanding the intricacy of Types
I think this has to be a very simple question but I am not
Maybe it is just a simple mistake but this thing is driving me crazy.

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.