# Javascript object and It's method

***Everything in javascript is an object.***

## What is an Object?

Object in simple words is an entity with properties and type.

It contains key-value pair. These key-value pairs are called properties.

## Accessing properties

* **Using Dot \[.\] operator**
    
    To access value we use the \[.\] operator followed by the property name (key).
    
    In the below example, "name" is the name of the property or a key and Bee is the value.
    
    ```javascript
    const obj = {name: "bee"}
    console.log(obj.name); // This will print bee
    ```
    
* **Using Square brackets \[\]**
    
    ```javascript
    const obj = {peanut: 20}
    console.log(obj['peanut']); // This will print 20
    ```
    
    In the above example, we use square brackets to access the value of the key.
    

## Object Initializing in javascript

* **Using Object Literals**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672149586142/e6f74a65-a879-4c7e-ba9a-28fff788a03c.png align="center")
    
* **Using Instance of Object**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672149394124/58f6b46b-f738-45a2-87e9-c4c8b0b5baee.png align="center")
    
* **Using constructor function**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672151660386/1845d730-09dc-4948-983d-1e32657a6767.png align="center")
    

## To check if the key exists or not

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672155674895/cb83ecba-347c-48d7-931c-9a8d3a8a9ac4.png align="center")

we use the "**in**" operator to check whether the key exists in the object or not

**in** returns either true or false;

In the above example,

name key is present inside the object so it printed true and since the email key was not present it printed false.

We can also use **hasOwnProperty** to check if the key is present or not

```javascript
if(obj.hasOwnPropert(key)){
    console.log("Present");
}else{
    console.log("Not Present");
}
```

## Creating Object Copies

1. **Assigning to an existing object.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672210806972/51a54d84-7280-4e89-81cf-e9dfa015dd4c.png align="center")
    
    In the above example, "newobj" named object is being made to point to the memory of object "obj". So It is **pointing by reference** which means the change made in "obj" will also be changed in "newobj" and vice-versa.
    
2. **Using spread operator.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672210875363/eafcba97-dce7-4950-b71a-ac59879a966b.png align="center")
    
    In the above example, we use spread operators(...) to copy object. The spread operator creates a **shallow copy** thus we see changes made "obj" doesn't reflect to "newobj".
    
3. **Using Object.Assign()**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672213381845/62880260-c3e5-4ecc-8404-04abe6536db5.png align="center")
    

**Object.assign()** takes **two** parameters

1. **target object** where we have to copy the properties.
    
2. **source object** from where we copy the properties.
    

Like spread operators, they also do the **shallow copy**.

## Looping over the object

1. **Using For in Loop**
    
    For in loop is used to iterate over object.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672159969334/a9576bc7-eb86-4e6d-b369-873b96e213f1.png align="center")
    
2. **Using Object.keys()**
    
    This method iterates over keys
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672160334551/5c717956-3f01-4f6d-859f-e1e345c0688f.png align="center")
    
3. **Using Object.values()**
    
    This method iterates over values
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672160366002/b09faeec-e4ee-4cea-93bf-6d39898c7806.png align="center")
    
4. **Using Object.entries()**
    
    This is another **ES8** method to iterate over objects.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672160664497/f59b475b-10e4-4a2c-bfde-cb12c33f16da.png align="center")
    

That concludes this article thanks for reading any feedback is welcomed.

**THANK YOU**
