How come if a = b, then b = a? [duplicate]












-4
















This question already has an answer here:




  • Copy array by value

    33 answers




Let me explain. I have the following piece of code :






let a = [1, 2, 3];
let b = a;
b.splice(2, 1);
console.log("a: " + a);
console.log("b: " + b);





I would have expected to get something like :



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


However, after running the code, it turns out both "a" and "b" equal [1, 2]. I am really confused, since "b" should only have been assigned a copy of "a", instead of acting as some sort of a pointer to "a". I wonder if it is because JS handles arrays (objects) differently of if it is something specific to the splice function. I would also like to know how you would bypass that odd behavior.



Thanks for your time.










share|improve this question













marked as duplicate by Denys Séguret javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 15:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 6





    b references a, it is not a copy. Someone will find the dupe.

    – epascarello
    Jan 2 at 15:14













  • "since "b" should only have been assigned a copy of "a" Why ? Where are you copying ?

    – Denys Séguret
    Jan 2 at 15:14






  • 2





    BTW copying would have been let b = a.slice();

    – Denys Séguret
    Jan 2 at 15:15











  • duplicate stackoverflow.com/a/6605700/4267015

    – Naor Tedgi
    Jan 2 at 15:16











  • @DenysSéguret what do you mean?

    – John Krakov
    Jan 2 at 15:32
















-4
















This question already has an answer here:




  • Copy array by value

    33 answers




Let me explain. I have the following piece of code :






let a = [1, 2, 3];
let b = a;
b.splice(2, 1);
console.log("a: " + a);
console.log("b: " + b);





I would have expected to get something like :



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


However, after running the code, it turns out both "a" and "b" equal [1, 2]. I am really confused, since "b" should only have been assigned a copy of "a", instead of acting as some sort of a pointer to "a". I wonder if it is because JS handles arrays (objects) differently of if it is something specific to the splice function. I would also like to know how you would bypass that odd behavior.



Thanks for your time.










share|improve this question













marked as duplicate by Denys Séguret javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 15:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 6





    b references a, it is not a copy. Someone will find the dupe.

    – epascarello
    Jan 2 at 15:14













  • "since "b" should only have been assigned a copy of "a" Why ? Where are you copying ?

    – Denys Séguret
    Jan 2 at 15:14






  • 2





    BTW copying would have been let b = a.slice();

    – Denys Séguret
    Jan 2 at 15:15











  • duplicate stackoverflow.com/a/6605700/4267015

    – Naor Tedgi
    Jan 2 at 15:16











  • @DenysSéguret what do you mean?

    – John Krakov
    Jan 2 at 15:32














-4












-4








-4


0







This question already has an answer here:




  • Copy array by value

    33 answers




Let me explain. I have the following piece of code :






let a = [1, 2, 3];
let b = a;
b.splice(2, 1);
console.log("a: " + a);
console.log("b: " + b);





I would have expected to get something like :



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


However, after running the code, it turns out both "a" and "b" equal [1, 2]. I am really confused, since "b" should only have been assigned a copy of "a", instead of acting as some sort of a pointer to "a". I wonder if it is because JS handles arrays (objects) differently of if it is something specific to the splice function. I would also like to know how you would bypass that odd behavior.



Thanks for your time.










share|improve this question















This question already has an answer here:




  • Copy array by value

    33 answers




Let me explain. I have the following piece of code :






let a = [1, 2, 3];
let b = a;
b.splice(2, 1);
console.log("a: " + a);
console.log("b: " + b);





I would have expected to get something like :



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


However, after running the code, it turns out both "a" and "b" equal [1, 2]. I am really confused, since "b" should only have been assigned a copy of "a", instead of acting as some sort of a pointer to "a". I wonder if it is because JS handles arrays (objects) differently of if it is something specific to the splice function. I would also like to know how you would bypass that odd behavior.



Thanks for your time.





This question already has an answer here:




  • Copy array by value

    33 answers







let a = [1, 2, 3];
let b = a;
b.splice(2, 1);
console.log("a: " + a);
console.log("b: " + b);





let a = [1, 2, 3];
let b = a;
b.splice(2, 1);
console.log("a: " + a);
console.log("b: " + b);






javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 15:12









John KrakovJohn Krakov

705




705




marked as duplicate by Denys Séguret javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 15:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Denys Séguret javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 15:16


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 6





    b references a, it is not a copy. Someone will find the dupe.

    – epascarello
    Jan 2 at 15:14













  • "since "b" should only have been assigned a copy of "a" Why ? Where are you copying ?

    – Denys Séguret
    Jan 2 at 15:14






  • 2





    BTW copying would have been let b = a.slice();

    – Denys Séguret
    Jan 2 at 15:15











  • duplicate stackoverflow.com/a/6605700/4267015

    – Naor Tedgi
    Jan 2 at 15:16











  • @DenysSéguret what do you mean?

    – John Krakov
    Jan 2 at 15:32














  • 6





    b references a, it is not a copy. Someone will find the dupe.

    – epascarello
    Jan 2 at 15:14













  • "since "b" should only have been assigned a copy of "a" Why ? Where are you copying ?

    – Denys Séguret
    Jan 2 at 15:14






  • 2





    BTW copying would have been let b = a.slice();

    – Denys Séguret
    Jan 2 at 15:15











  • duplicate stackoverflow.com/a/6605700/4267015

    – Naor Tedgi
    Jan 2 at 15:16











  • @DenysSéguret what do you mean?

    – John Krakov
    Jan 2 at 15:32








6




6





b references a, it is not a copy. Someone will find the dupe.

– epascarello
Jan 2 at 15:14







b references a, it is not a copy. Someone will find the dupe.

– epascarello
Jan 2 at 15:14















"since "b" should only have been assigned a copy of "a" Why ? Where are you copying ?

– Denys Séguret
Jan 2 at 15:14





"since "b" should only have been assigned a copy of "a" Why ? Where are you copying ?

– Denys Séguret
Jan 2 at 15:14




2




2





BTW copying would have been let b = a.slice();

– Denys Séguret
Jan 2 at 15:15





BTW copying would have been let b = a.slice();

– Denys Séguret
Jan 2 at 15:15













duplicate stackoverflow.com/a/6605700/4267015

– Naor Tedgi
Jan 2 at 15:16





duplicate stackoverflow.com/a/6605700/4267015

– Naor Tedgi
Jan 2 at 15:16













@DenysSéguret what do you mean?

– John Krakov
Jan 2 at 15:32





@DenysSéguret what do you mean?

– John Krakov
Jan 2 at 15:32












1 Answer
1






active

oldest

votes


















2














In the first line you are creating the array and pointing a variable to that array.
In the second line let b = a;, here you are pointing b variable to the a array again.



So both a and b variables will be pointing to the same array. When you do a change to the array, both a and b values will be changed.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    In the first line you are creating the array and pointing a variable to that array.
    In the second line let b = a;, here you are pointing b variable to the a array again.



    So both a and b variables will be pointing to the same array. When you do a change to the array, both a and b values will be changed.






    share|improve this answer




























      2














      In the first line you are creating the array and pointing a variable to that array.
      In the second line let b = a;, here you are pointing b variable to the a array again.



      So both a and b variables will be pointing to the same array. When you do a change to the array, both a and b values will be changed.






      share|improve this answer


























        2












        2








        2







        In the first line you are creating the array and pointing a variable to that array.
        In the second line let b = a;, here you are pointing b variable to the a array again.



        So both a and b variables will be pointing to the same array. When you do a change to the array, both a and b values will be changed.






        share|improve this answer













        In the first line you are creating the array and pointing a variable to that array.
        In the second line let b = a;, here you are pointing b variable to the a array again.



        So both a and b variables will be pointing to the same array. When you do a change to the array, both a and b values will be changed.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 15:16









        SandSand

        1,7213822




        1,7213822

















            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]