This kind of method can also be applied to more common requirements, such as modifying an object. This is a very good idea for, say, Redux users: a reducer can be programmed so that it will receive the old state as a parameter and produce an updated version with the minimum needed changes, without altering the original state in any way.
Imagine you had the following object:
myObj = { a: ..., b: ..., c: ..., d: { e: ..., f: ..., g: { h: ..., i: ... } }};
Let's assume you wanted to modify the value of the myObj.d.f attribute, but working in a persistent way. Instead of copying the full object (with the deepCopy() function that we used earlier), we could create a new object that has several attributes in common with the previous ...