var list={element:"1",next:{element:"2",next:null}}
var current=list
current.next.next={element:"3",next:null}
current.next.next.next={element:"4",next:null}
console.log(JSON.stringify(current))
console.log(JSON.stringify(list))
I wrote this code myself. I don’t understand one thing how my list variable is getting updated
because
first I say
var list={element:"1",next:{element:"2",next:null}}
then I assign that value to variable current
var current=list
after that you can see on the code that I only change the value of variable current
I did not say anywhare
list=current
but when I say
console.log(JSON.stringify(current))
console.log(JSON.stringify(list))
both current and list variable look similar
below is the output we get when we use
console.log(JSON.stringify(current))
console.log(JSON.stringify(list))
{"element":"1","next":{"element":"2","next":{"element":"3","next":{"element":"4"
,"next":null}}}}
anybody can explain this?
see the below code
var num1=5
var num2=num1
num2=6
console.log(num1)
// produce output 5
console.log(num2)
//produce output 6
here I say
var num2=num1
// now the value of num2 is 5
then I say
I say
num2 =6
//now the value of num2 is 6
//but the value of num1 is still 5
//it did not change
// this thing I know why the value of num1 is not changed
// but the first program I wrote made me confused
// please help me