DEV Community

Mohamed Idris
Mohamed Idris

Posted on

Things I learned recently...

  • const obj1 = obj2;

obj1 is now a reference for obj2, it just pointing to obj2. So, modifying obj2 will also modify obj1

If you want to be able to modify obj1 freely, a simple solution is by destructuring:

const obj1 = {...obj2};


  • const obj3 = {a: 2, b: 3, c: 4}, and you want to exclude or delete the 'b' attribute? :

delete obj3['b'];


  • Destructing with an alias!

const obj4 = {name: "Idris"};

if we want to set the name attribute to a new variable called username, we can do it like this:

const {name: username} = obj4;

console.log(username); // Idris

Top comments (0)