4 MAIN JAVASCRIPT Object Methods That Every Developer Should Know
Tags: webdev, javascript, programming, tutorial
Published: 2023-11-18 04:08:15

Welcome to the world of JavaScript, where coding is like creating a beautiful piece of music. In this blog, we're going to explore 4 main JavaScript Object Method that come handy most of the time in our programming world of Objects in JavaScript..

- <u>**Object.keys(obj_refrence)**</u> :--

    _Object.keys() returns an array of given object's property 
    names._

    In JavaScript, objects can store information in key-value 
    pairs, like a dictionary. Now, imagine you have an object with 
    a bunch of keys (names) and values (associated information).

    Object.keys() is like a tool that helps you get only the keys 
    from that object. It's saying, "Give me a list of all the 
    names (keys) in this object."

    **Here's a simple example:**
    
    
   ```
     const obj = { a: 1, b: 2, c: 3 };
     console.log(Object.keys(obj));
     // Output: ["a", "b", "c"]
   ```

- <u>**Object.values(obj_refrence)**</u> :--

    _Object.values() returns an array of given object's value._ 

    Here, we will try to access the value of the object instead of 
    name just like above.

    Object.values() is like a tool that helps you get only the 
    values from that object. It's saying, "Give me a list of all 
    the values (associated information) in this object."

    **Here's a simple example:**
    
    
   ```
     const obj = { a: 1, b: 2, c: 3 };
     console.log(Object.values(obj));
     // Output: [1,2,3]
   ```

- <u>**Object.entries(obj_refrence)**</u> :--

    _Object.entries() returns an array of key-value pairs._ 

    Imagine you have an object, and it's like a treasure chest 
    with many items inside. Each item has a key (name) and some 
    value (associated information).
 
    Now, Object.entries() is a special tool that helps you see 
    both the names and the association of each item in the chest.

    **Here's a simple example:**
    
    
   ```
     let treasureChest = {
       goldCoins: 50,
       silverBars: 20,
       preciousGems: ['diamond', 'sapphire', 'ruby']
     };

     console.log(Object.entries(treasureChest));

     //OUTPUT:  [['goldCoins', 50],['silverBars', 20],
                ['preciousGems', ['diamond', 'sapphire', 'ruby']]]

   ```

- <u>**Object.assign(target_object,src_1,....,src_n)**</u> :--

    _Object.assign() Copies key-value pair from one or more source 
    objects to target objects._


    It helps you create a new object by combining the properties 
    of one or more existing objects. It's useful when you want to 
    merge information from different sources into a single, 
    consolidated object in your JavaScript code.


    Imagine you have two containers, one with items (target) and 
    another with more items (source). Object.assign() is like a 
    helper that takes items from the second container (source) and 
    adds them to the first container (target), creating a new, 
    bigger container (result).

    **Here's a simple example:**
    
    
   ```
      const target = { a: 1, b: 2 };
      const source = { c: 4, d: 5 };
      const result = Object.assign(target, source);
      console.log(result);
      // Output: { a: 1, b: 2, c: 4, d: 5 }

   ```

---

##SUMMARY

And there you have got it—our journey via JavaScript's magical toolbox! 

We've met four pleasant helpers: one that suggests us names (Object.Keys), any other that sings us values (Object.Values), a treasure hunter revealing both names and values (Object.Entries), and a master mixer developing a new blend of objects (Object.Assign).

As we wrap up, remember, coding is an ongoing adventure. _Follow me_ for more discoveries, and share your thoughts below. Let's keep the coding conversation alive! 🚀🎉