# Objects in javascript

### Javascript objects

Th object type represents on of the javascript datatypes. It is used to store various key value pairs and much more complex entities.

Objects can be created using the object(), constructer or the object initializer/literal syntax. In javascript almost everything is a object.

* Boolean, number, string can also be an object if defined with new keyword.
    
* Dates, maths, regularexpressions, array , functions amd objects are always objects.
    

`IMP: All javascript values except primitives, are objects.`

#### Primitive value: A primitive value is a value that has no properties or methods of itself.

JS has 7 types of primitive values

1. String
    
2. Number
    
3. Boolean
    
4. Null
    
5. undefined
    
6. Symbol
    
7. bigint
    

#### Object methods: Methods are the actions that can be performed on objects. Object properties can be both primitive values other objects or any function.

#### Creating a javascript object:

1. By using and object literal.
    
2. By using the new keyword.
    
3. Define and object constructor, and then create object of the constructed type.
    
4. Creating object using `object.create`.
    

#### 1\. By using and object literal.

```plaintext
const info={
name:"Abhay",
age:"22",
address:"noida"
}
```

#### 2\. By using the new keyword.

```plaintext
const info= new object{};
 info.name:"Abhay",
 info.age:"22",
 info.address:"noida"
```

#### 3\. Define and object constructor, and then create object of the constructed type.

```plaintext
function Person(first, last, age, eye) {
  this.firstName = first;
  this.lastName = last;
  this.age = age;
  this.eyeColor = eye;
}
```

#### 4\. Creating object using `object.create`.

The Object.create() static method creates a new object, using an existing object as the prototype of the newly created object.

```plaintext
Object.create(prototype[, propertiesObject])
```

Parameters:

1. prototype: It is the prototype object from which a new object has to be created.
    
2. propertiesObject: It is an optional parameter. It specifies the enumerable properties to be added to the newly created object.
    

```plaintext
const person = {
  isHuman: false,
  printIntroduction: function() {
    console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
  }
};

const me = Object.create(person);

me.name = 'Matthew'; // "name" is a property set on "me", but not on "person"
me.isHuman = true; // Inherited properties can be overwritten

me.printIntroduction();
// Expected output: "My name is Matthew. Am I human? true"
```

### Javascript object properties :

#### Accessing javascript object properties:

```plaintext
const info={
name:"Abhay",
age:"22",
address:"noida"
}
info.name;
//expected output Abhay
info.age;
//expected output 22
```

or

```plaintext
const info={
name:"Abhay",
age:"22",
address:"noida"
}
info["name"];
//expected output Abhay
info["age"];
//expected output 22
```

#### How to iterate through objects:

```plaintext
const info={
name:"Abhay",
age:"22",
address:"noida"
}
for(let x in info){
console.log(info[x]);
}
```

#### How to add new properties to object: We can simply add new properties to an existing object by simply assigning it a value.

```plaintext
const info={
name:"Abhay",
age:"22",
address:"noida"
}
info.number="1234";
//1234 will be added to info object as a key value pair
```

#### How to delete properties of an object: We can simply delete properties of an existing object by simply using Delete keyword.

```javascript
const info={
name:"Abhay",
age:"22",
address:"noida"
}
Delete info.age;
//age property will get deleted from the info object
```

#### Nested objects: Multiple objects can be nested inside the each other in javascript.

```javascript
const info={
name:"Abhay",
age:"22",
address:"noida"
bikes:{
redbike:"harley",
blackbike:"suzuki"
}
}
console.log(info.bikes.redbike);
//Expected output: harley
```
