This exercise is not mandatory, however it may be a good example for understanding the "variables created in functions" and concept of "passing objects (object, arrays and functions) by reference" in JavaScript.
Given the below code, without running the code in the console try to answer what will be the output of 2 console.log
statements on the bottom.
function magicHat(obj) {
obj.age = 10;
obj = { name: 'John', age: 20 };
return obj;
}
let rabbit1 = { name: 'Rapha', age: 30 };
let rabbit2 = magicHat(rabbit1);
// console.log('rabbit1 ->', rabbit1); // -> ?
// console.log('rabbit2 ->', rabbit2); // -> ?
π
If you were unable to come up with the answer after some time, and/or you are not a fan of solving riddles π check this link with the answer and explanation.