I am currently learning javascript, i create an array and then define it in 2 variable arrayBase and arrayData but when i use splice, all the data in other variable also get removed. I don't know what happen
const arr=[1,2,3,4,5]
const arrayBase=arr
let max=0
let min=0
let arrayData=arr
for(let i = 0; i<arrayBase.length;i++){
console.log(i,arrayBase)
let x
arrayData.splice(i,1)
arrayData.forEach((item)=>x=x+item)
if(x>max){
max=x
}
if(min<x){
min=x
}
arrayDataabc=arr
}
Can anyone help explain why splice make other variable array get removed? Thanks before!
In Javasxript Objects (even arrays are object) are mutable means it can be modified .
when you assign the objects (even array) to the new variable , the new variables point to the same objects.