Objects in Javascript and some of its methods
Published: 2021-07-21 11:31:03

Objects are very `important` in Javascript

So, what are Javascript objects? 
Objects in Javascript are a collection of named properties with values. 

Here's an example 

```javascript
let obj = {
   firstname: "alex",
   lastname: "muriuki",
   age: 25
}
```
 
😁 And that's it you have an object.
There are a couple of various methods we can use to play around with properties inside an object. 

`1. Object.keys()`
This method returns an array of all key string properties of the said Object. Very handy if you would want to iterate through all key properties and do whatever you would wish with each iteration.😵

> Here's an example

```javascript
let obj = {
   firstname: "alex",
   lastname: "muriuki",
   age: 25
}

Object.keys(obj) // ['firstname', 'lastname', 'age']
```


`2. Object.entries()`
This one is interesting. The said method returns an array containing the key value pairs in the object. Try it out and mess around with it

> Here's an example

```javascript
let obj = {
   firstname: "alex",
   lastname: "muriuki",
   age: 25
}

Object.entries(obj)
/*
[
  ['firstname', 'alex'],
  ['lastname', 'muriuki']
  ['age', 25]
]
*/
```


`3. Object.freeze()`
We can make an object to be immutable. Something mutable can be changed while on the other hand something immutable cannot be changed. This method comes in handy when we want our Object not to be affected by any code in our file. This can be super handy if you'd want to ensure data integrity, prevent unintended modifications, or enable safe concurrent access to some data object.

> Here's an example

```javascript
let obj = {
   firstname: "alex",
   lastname: "muriuki",
   age: 25,
   bankbalance: 2400
}

Object.freeze(obj)
obj.bankbalance = 100000 // will be ignored
delete obj.age // will also be ignored
```

`4. Object.values()`
What if we want to get all properties in an object. 🤔.
to the rescue. This method returns an array of all string properties in the object. It may be helpful if you would want access to all string values in any object

```javascript
let obj = {
   firstname: "alex",
   lastname: "muriuki",
   age: 25
}

Object.values(obj) // ['alex', 'muriuki', 25]
```


😎Bonus
There is also some additional things you can do with Javascript objects

* Add an object property

Going back to our example above. We can add an object by
 
```javascript
obj.location = "Nairobi"
```

And just like that our obj has 4 properties now. We can add infinitely many properties to this object so feel free to do so.🤗

* Delete an object
Just the same way we added a property to the obj above we can also delete a property from an object using the delete keyword
  
```javascript
delete obj.lastname
```
And yeap! The property has been deleted 🎉

* We can also change the structure of a property in an object. How do we do it? It's indeed simple 
We may decide to update the location value from a string to an array like so

```javascript
obj.location = [
  "Kenya",
  "Nairobi"
]
```


This is not an exhaustive list of the methods present in the `Object`.
For more reading you can visit MDN Javascript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

Thanks for reading.🤝