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:
- Original text!
- theData: {text: "Text Changed!"}
- Text Changed!
- newData: {text: "Text Changed!"}
My question is why the second console.log did not log: theData: {text: "Original text!"}?