Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Objects Are Passed by Reference

Behind the Scenesmedium

Course · Section 8: How JavaScript Works Behind the Scenes · Lecture 104: Object References in Practice (Shallow vs. Deep Copies)

Implement `addAge(person, age)` that sets the `age` property on the passed object and returns it. Objects are shared by reference.

Sample tests

Input: addAge({"name":"A"}, 30)
Output: {"name":"A","age":30}
Input: addAge({"name":"B","age":1}, 2)
Output: {"name":"B","age":2}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • In React state you must NOT mutate like this; here it demonstrates reference semantics.
Visualize this concept: Primitives vs. References →
Approach & explanation (try first)

Because objects are passed by reference, assigning a property changes the caller's object.

Loading...
⌘/Ctrl + Enter

Run your code to see results.