How come if a = b, then b = a? [duplicate]
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.
javascript arrays
marked as duplicate by Denys Séguret
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.
|
show 4 more comments
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.
javascript arrays
marked as duplicate by Denys Séguret
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
referencesa
, 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 beenlet 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
|
show 4 more comments
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.
javascript arrays
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
javascript arrays
asked Jan 2 at 15:12
John KrakovJohn Krakov
705
705
marked as duplicate by Denys Séguret
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
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
referencesa
, 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 beenlet 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
|
show 4 more comments
6
b
referencesa
, 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 beenlet 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
|
show 4 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 2 at 15:16
SandSand
1,7213822
1,7213822
add a comment |
add a comment |
6
b
referencesa
, 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