JavaScript objects and it’s secrets you should know!!

Rahul Dutt Pandey
4 min readApr 5, 2021

objects are one of the JavaScript’s data types. It can be seen as a collection of properties. objects can be created using object() construct or Object.create().

There are various object methods that can be used we will discuss the few import ones here.

What is copying of objects ?

we saw what is creation of object and when we use these objects sometimes we need to store the value in a different variable to preserve the value. This process of creating a new variable with the old value is known as copying. Let’s see what are the conditions and challenges in copying an object.

Shallow Copy & Deep Copy

Shallow Copy:

Objects can not be simply copied using the very famous ‘=’ operator. I know you must be wondering why? So what happens behind the scene is that when we use the ‘=’ operator the objects keep sharing the same memory location and when we make changes to any one of them it reflects for both of the objects. Lets see what it looks like on our code panel.

To overcome this issue we can use 2 different ways of copying objects : —

  1. using spread operator(….):

we can copy the old object to a new one using the spread operator and it will create a new location as well but it will also fail if there is a nested object case.

2. using object.assign():

this method copies all the enumerable and own properties from one or more than one sources to the target object. It returns the target object. This method will also fail if the object is nested.

Deep Copy :

So we saw that when we are trying to make a copy of object using the spread operator or the assign method and it is a nested object we fail to fulfil the need . The best way to do a deep copy is to do it manually copy each one manually . Ok don’t panic I was just joking . We can just stringify the object and parse it just after. let’s see how it’s done:

Some methods to make things simple for your javascript journey:

  1. Object.keys():

This method will create and return an array of keys from the object. It can be used to iterate through an object and it can also be helpful if you want to find the length of an object.

2. Object.values():

Just like the keys() functions makes an array of all the keys , the values() function creates and returns an array of all the values.

3. Object.entries():

This method creates a nested array of key/values of the object and it will only return the object instance’s own properties, and not any properties that may be inherited through its prototype. Once we have the array we can use forEach to loop over and play around.

4.Object.freeze():

This method as the name suggests freezes the operations on an object. We can not change any value or add or remove any value from the objects. Freezing helps protect the enumerability, configurability, or writability of existing properties, and prevents the values of existing properties from being changed. This method returns the same object that is being passed. You can use object.isFrozen() to check if the object is frozen or not it simply return a Boolean value.

5. Object.seal():

This method is a lite version of filter(). This actually stops user from adding any new properties but allows to modify the existing properties. There is object.isSealed() to check if the object is sealed.

Conclusion:

There are a lot objects methods that are very useful in performing modifications, iterations and a lot more. In this blog I have tried to cover major part of those methods. You can learn more about the methods here.

If you found this article interesting or maybe if something cool pops in your head after reading this do let me know on LinkedIn or know more about me here.

--

--

Rahul Dutt Pandey

I'm an Engineer who loves to play with code and give shape to my ideas.