0

I am unable to understand what is happening in the following code. first i create the object then console the object before modifying it. but instead it output the modified value.

 <script>
   var x={
      "name" :"nauman",
      "address":{
         "add1":"home",
         "add2": "home2"
        }
    }
  console.log(x);// it log hom3 instead of home
  x.address.add1="hom3"; 
      console.log(x);
  </script>
Nauman Ahmad
  • 320
  • 4
  • 17
  • the JS console on some browsers does late evaluation of the contents of objects – Alnitak Apr 02 '16 at 12:10
  • Not over here. Everything working fine in Chrome and in FF too. – John Apr 02 '16 at 12:12
  • This question is outlined here: http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays – Arend Apr 02 '16 at 12:13
  • @John which browser u r using ?? – Nauman Ahmad Apr 02 '16 at 12:13
  • Chrome 49 and Firefox 45. – John Apr 02 '16 at 12:14
  • The answers to the duplicate questions seem to suggest this is some kind of bug. It's not. It is expected behavior. The console holds a live reference to the object (as it should). Not a clone. If you mutate the reference, you also mutate the one in the console. It would probably be helpful to read about by references vs by value in javascript https://stackoverflow.com/questions/6605640/javascript-by-reference-vs-by-value – Charlie Martin Dec 08 '17 at 05:04
  • @CharlieMartin thanks. now i understand how this works. it is one year old question was just editing it so that it might help some body else. – Nauman Ahmad Dec 08 '17 at 05:08

0 Answers0