DEV Community

Cover image for Objects in Javascript and some of its methods
Alex
Alex

Posted on • Updated on

Objects in Javascript and some of its methods

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

let obj = {
   firstname: "alex",
   lastname: "muriuki",
   age: 25
}
Enter fullscreen mode Exit fullscreen mode

😁 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

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

Object.keys(obj) // ['firstname', 'lastname', 'age']
Enter fullscreen mode Exit fullscreen mode

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

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

Object.entries(obj)
/*
[
  ['firstname', 'alex'],
  ['lastname', 'muriuki']
  ['age', 25]
]
*/
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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

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

Object.values(obj) // ['alex', 'muriuki', 25]
Enter fullscreen mode Exit fullscreen mode

😎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

obj.location = "Nairobi"
Enter fullscreen mode Exit fullscreen mode

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
delete obj.lastname
Enter fullscreen mode Exit fullscreen mode

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
obj.location = [
  "Kenya",
  "Nairobi"
]
Enter fullscreen mode Exit fullscreen mode

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.🀝

Top comments (0)