Why does the following code snippet below have such a result? variables of type CONST could not have their value changed right?
const theDog = { name: 'Totó', guardian: { name: 'Alan Turing' } } const otherDog = { ...theDog, name: 'Tulipa' } otherDog.guardian.name = 'Maria Luiza' theDog? { name: 'Totó', guardian: { name: 'Maria Luiza' } }
const otherDog = { ...theDog, guardian: {...theDog.guardian}, name: 'Tulipa' }to avoid this issue in your example.otherDogis a shallow copy oftheDog. Check out What is the difference between a deep copy and a shallow copy? and/or have a look at the Wikipedia page about shallow copying.