clone an object in javascript using spread operator code example
Example: javascript clone object
// syntax: let <newObjectName> = {...<initialObjectName>};
// example:
const me = {
name: 'Jakes',
age: 30,
};
const friend = {...me};
friend.age = 27;
console.log(friend.age); // 27
console.log(me.age); // 30
// -----------------------------
// BAD
// -----------------------------
const me = {
name: 'Jonas',
age: 30,
};
const friend = me;
friend.age = 27;
console.log(friend.age); // 27
console.log(me.age); // 30