what is immutability in js code example

Example 1: 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)

Example 2: immutable values

let a = {
    foo: 'bar'
};

let b = a;

a.foo = 'test';

console.log(b.foo); // test
console.log(a === b) // true
let a = 'test';
let b = a;
a = a.substring(2);

console.log(a) //st
console.log(b) //test
console.log(a === b) //false
let a = ['foo', 'bar'];
let b = a;

a.push('baz')

console.log(b); // ['foo', 'bar', 'baz']

console.log(a === b) // true
let a = 1;
let b = a;
a++;

console.log(a) //2
console.log(b) //1
console.log(a === b) //false