three ways to clone objects in javascript

Since objects in JavaScript are references values, you can't simply just copy using the assign  = following are the 3 ways for you to clone an object.

1. Using JSON

const car = { tyre: '4', seats: '5' };

const cloneCar = JSON.parse(JSON.stringify(car));

console.log(cloneCar );
// { tyre: '4', seats: '5' };

2. Using Object.assign

const car = { tyre: '4', seats: '5' };
const cloneCar = Object.assign({}, car);
console.log(cloneFood);
// { tyre: '4', seats: '5' };
 
 
3. Using Spread
const food = { tyre: '4', seats: '5' };
const cloneCar = { ...car};
console.log(cloneCar );
// { tyre: '4', seats: '5' };