1
Good afternoon, I have this little program:
let oi=[1,2,3,4]
let xau=[]
xau=oi
xau[2]=7
console.log(oi)
my problem is that when on the console the impression that comes out is:
[1,2,7,4]
according to what I understand, xau should be a new instance that has the values of hi, but when modifying xau, tbm modifico hi, if someone can explain to me why this is happening and how to do so that I can modify xau without modifying hi, I am grateful
thanks mt, did not know it was passed by reference, helped me mt
– Thiago Outeiro Pereira Damasce
The answer is great, I’ll even vote +1. But just allow me one observation: the value
0
in theslice
would be unnecessary. If you omit the parameters (leave empty,slice()
) the value0
is already assumed.– Sam
@dvd edited. Thank you for the suggestion.
– rdleal