0

Can someone please explain the execution of below code in JavaScript:

function modifyData(data) {
    let data2 = data;

    data2.text = "Text Changed!";
}

let data = {
    text: "Original text!"
}

console.log(data.text);
console.log({theData: data});

modifyData(data);

console.log(data.text);
console.log({newData: data});

Executing above script logs console values in below order:

  1. Original text!
  2. theData: {text: "Text Changed!"}
  3. Text Changed!
  4. newData: {text: "Text Changed!"}

My question is why the second console.log did not log: theData: {text: "Original text!"}?

  • It has to do with how the browser's debugger works, use `console.log(JSON.stringify({theData: data}))` and you'll get the expected result. – Titus Apr 02 '20 at 10:46
  • Thank you @Titus for the suggestion. For others who may find this question and looking for an answer, please look at the links on top provided by VLAZ and Bergi. – Indiwar Jha Apr 02 '20 at 10:59

0 Answers0