javascript immutability code example
Example 1: string immutable javascript
var statement = "I am an immutable value";
var otherStr = statement.slice(8, 17);
Example 2: js immutability
const userProfile = {
username: 'Jack',
email: 'Jack@email',
socialMedia: {
twitter: '@jack',
facebook: 'jackm',
linkedIn: 'jackmay',
},
}
const newUserProfile = {
...userProfile,
email: 'JackNew@email',
socialMedia: { ...userProfile.socialMedia, twitter: '@newTwitter' },
}
console.log('orginal', userProfile)
console.log('new', newUserProfile)