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' },
}
// const newUserProfile = JSON.parse(JSON.stringify(userProfile))
console.log('orginal', userProfile)
console.log('new', newUserProfile)