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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:31:18+00:00 2026-06-17T03:31:18+00:00

Possible Duplicate: Copying array by value in javascript i have a funny problem with

  • 0

Possible Duplicate:
Copying array by value in javascript

i have a funny problem with javascript. i copy an array variable to make modifications on the copy only, then splice the copy to delete an element. however the original array variable is affected by the splice – as if the copy was a ‘copy by reference’:

window.onload = function() {
  var initial_variable = ['first', 'second', 'third'];
  var copy_initial_variable = initial_variable;
  copy_initial_variable.splice(0, 1);
  alert('initial variable - ' + initial_variable);
};
//output: initial variable - second,third

firstly, is this intentional behaviour for javascript or is it a bug?

and secondly, how can i make a copy of an array and delete an element in the copy but not in the original?

one thing which makes me think that the above may be a javascript bug is that this behaviour only happens with arrays and not with integers. for example:

window.onload = function() {
  var initial_variable = 1;
  var copy_initial_variable = initial_variable;
  copy_initial_variable = 2;
  alert('initial variable - ' + initial_variable);
};
//output: initial variable - 1

if the behaviour were consistent then this ought to output 2 since the assignment would presumably be by reference?

  • 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-17T03:31:19+00:00Added an answer on June 17, 2026 at 3:31 am

    This is in no way a bug, but a very common misunderstanding. Let’s see what happens when I say

    var a = b;
    

    Integers and other javascript primitives, like floats and booleans, are “assigned by value”.
    Which means that whatever value b has is going to be copied to a. To the computer, it means having the part of memory that b references copied to the memory that a references. That’s the behavior you were expecting.

    When arrays and other objects (and “descendants” of a new Object() call) are used like that, there is a copy by reference. Meaning that the value of a now references the value of b, the memory that b references isn’t copied or modified. Thus, when writing

    a = [1,2,3];
    b = a;
    

    b and a become interchangeable. They’re referencing the same memory address. To achieve what you’re trying to do, use

    var copy_initial_variable = initial_variable.slice(0);
    

    Read Does JavaScript pass by reference? for more information.

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

Sidebar

Related Questions

Possible Duplicate: Copying array by value in javascript How to copy an array of
Possible Duplicate: Java: recommended solution for deep cloning/copying an instance I have an object
Possible Duplicate: deep copy NSMutableArray in Objective-C ? I have two arrays of length
Possible Duplicate: Copying one structure to another I have a struct defined as struct
Possible Duplicate: What is the most efficient way to clone a JavaScript object? Copying
Possible Duplicate: byte array to Int Array I have a large array of bytes,
Possible Duplicate: Mercurial: copying ONE file and its history to another repository I have
Possible Duplicate: What's the best way to make a deep copy of a data
Possible Duplicate: std::vector<std::string> to char* array I have to call a c function that
Possible Duplicate: What's the best way to make a deep copy of a data

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.