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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:59:35+00:00 2026-06-03T04:59:35+00:00

Note: this isn’t one of the millions dups of the common array copy ‘problem’

  • 0

Note: this isn’t one of the millions dups of the common array copy ‘problem’ where using arr.slice(0) fixes this ‘problem’

That said, I want to understand why I am getting this unexpected result:

var oldArr = [[1,2],[3,4]];
var find = oldArr[1];

var newArr = oldArr.slice(0);
console.log(newArr.indexOf(find)); //1?

//proof that newArr is NOT referenced to oldArr
newArr[0] = "Hi";
newArr[1] = "How are you?";
console.log(oldArr+" "+newArr); //"1,2,3,4 Hi,How are you?"

jsFildde Demo

If you replace find with any of the following alternatives, it returns the expected -1:

  • Use [3,4] directly
  • Use a variable holding [3,4]
  • Use a variable with reference of another array holding [3,4]

I can’t find any explanation on why there is any difference between these last three methods and the first example. As far as I know, there shouldn’t be any.

Any ideas?

  • 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-03T04:59:36+00:00Added an answer on June 3, 2026 at 4:59 am

    In:

    [[1,2],[3,4]]
    

    There are three array objects created.

    Only the outer one is being slice‘d. This results in a “shallow copy”.

    Consider this:

    var a = [1,2]
    var b = [3,4]
    var c = [a,b]
    var d = c.slice(0)
    d[0] === a       // true, which means it is the /same/ object
    d[0][0] = "Hi!"
    a                // ["Hi!", 2]
    

    Happy coding!

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

Sidebar

Related Questions

Note that this isn't a problem I face, I'm more interested in what is
Note: This problem occurs only on Mac, on Windows works fine. I have a
Note: this question is related to this one , but two years is a
Note: This is asking for the reverse of the usual tuple-to-array conversion. I have
Is there an easier way to do this in Python (2.7)?: Note: This isn't
Note: this was inspired by WebBrowser Event Properties? Why am I able to access
Note this question was originally posted in 2009, before C++11 was ratified and before
Note: This question has broadened in scope from previous revisions. I have tried to
Note: This issue appears to be limited to SQL Server 2005 SP2 I have
Note: this is not real information: $ ssh-keygen -t rsa -C "tekkub@gmail.com" Generating public/private

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.