Things Every Beginner Needs to Know About Objects In JavaScript
Tags: webdev, javascript, beginners, web3
Published: 2023-07-08 07:30:09

**Prerequisite**
For the reader to understand the contents of this article, it is essential to
have an understanding of HTML, CSS, and basic JavaScript.


**Table of Contents**
•	Introduction
•	What are objects?
•	Creating objects in JavaScript 
•	Accessing and modifying the property and value of objects in JavaScript 
•	Object method and the ‘this’ keyword
•	When to use objects in our code
•	conclusion

**Introduction**
Data types and data structures are common to all programming languages. In JavaScript, data types include; numbers, strings, Booleans, undefined, null, _bigint_, and symbols. Other data structures include objects, arrays, maps, and sets. This article will explain what objects are, basic object syntax and how they are created, how to access properties and methods of objects, object methods, the 'this' keyword, and when it should be used.

**What are objects?**
Objects are a non-primitive data type used to store complex collections of data. It functions as an entity that stores data in key-value pairs. It consists of functions and variables, which are called methods and properties. Just like an array, any expression can be input into an object value. Thus when creating objects, you may choose to give them a static value called a property or a dynamic value called a method.

**Creating objects in JavaScript**
There are various ways of creating objects. They can be created by using the object literal syntax, constructor functions, ECMAScript 6 classes, and the __object.create_  method. Let us examine them individually.
1.	**Object literal syntax**
This is the most obvious way of creating objects in JavaScript. It involves declaring a variable and giving the values assigned to the variable a name. This is what we mean when we say an object stores data in key-value pairs. For example, the code below has three properties; _name,_ _job_, and _birthYear_, and a method _calcAge_: 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvbtnozd39pop1a6nifr.png)


This syntax enables values to be assigned names called keys. For example, the key ‘name’ has a value of 'John Samuels.' This is what JavaScript object syntax looks like.
2.	**Constructor functions**
Another way of creating an object is by using a function to build an object. This function is called a constructor function. A function expression or a function declaration can be used to create a constructor function. However, an arrow function will not work as a constructor function simply because it does not have its own ‘this’ keyword which is essential to a constructor function. The function is then called with the new operator. 

In the code below, a function called ‘Individual’ is created with the parameters of _name,_ _job,_ and _birthYear_. The function is then called using the new operator. When the new operator is used to call a constructor function, like in the code below, an empty object is created, and the 'this' keyword is set to that empty object which is then linked to a prototype and returned by the function. If you take a look at the code below, the properties are declared with the 'this' keyword inside the constructor function, and the new empty object will then contain all those properties, which are then returned as _john()._


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ly0h0l09red7exph3ggi.png)

 

We can also define methods on the constructor function which will then be accessed by every object created by that constructor function. This is generally known as prototypal inheritance. The prototype property exists on all functions, including a constructor function. For example, in the code below, the _calcAge _method has been defined on the prototype property of the constructor function _Individual._ The _john _object, which has been created using the constructor function, will get access to the _calcAge_ method.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxv10boaaswkclbk69ms.png)


3.	**ES6 Classes**
ECMAScript 6 introduced the use of the class keyword to create objects in JavaScript. ES6 classes allow us to do the same thing we did on the constructor function using a more modern syntax. A class is like a function, and like functions, a class can be created by a class expression or declaration. 

In the code below, an _Individual _class is created with a constructor method inside it. The constructor method functions like a constructor function declaring the properties with the ‘this’ keyword. The _calcAge_ method is defined outside the constructor method, and it is automatically added to the prototype property of the class, which can then be inherited by any object created using the class _Individual_.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/818j4vr0pka4a12qrrqm.png)

 
4.	**Object dot create method**.
_Object.create_ is a function that works differently from ES6 classes and constructor functions. Prototypal inheritance does exist. However, the new operator is not used to call the function, and constructor functions are not needed. Instead, the _object.create_ function is used to manually set the prototype of an object to any other object we create. 

In the code below, an object called _individual_ is created using the object literal syntax. A method called property which contains the parameters of the properties to be set is defined inside the object. The _calcAge_ method is also defined in the object. An object called _john_ is then created, and the _object.create _method is used to set the prototype of this empty object to the Individual object. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ns9a2y5irjkvpcnqtkbf.png)

 
The _object.create_ method is important as it can also be used to implement inheritance between classes.

**Accessing and modifying the property and value of an object**
We have seen from the above illustrations the various ways of creating objects. It is also important for you to know how to access and modify the property and value of an object. There are two common ways of doing this: using the dot notation and the bracket notation. Apart from these two ways, the value of an object can also be accessed by destructuring. Let us consider them one by one.
1.	**Dot notation**
This way of accessing data from an object is straightforward. All we have to do is use the dot operator between the object and the property we want to access. For example: In the code below, the property name was retrieved from the object using the dot notation, and it was then logged to the console.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfzpghj4s9dmx0f206t4.png)

 
The dot can also be used to add new properties to the object. For example, the code below adds a property called _birthYear _with a value of ‘2002’ to the _john_ object using an equal to operator.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spszliopv3bkw62smu0l.png)

 
2.	**Bracket notation**
Properties are accessed or added to objects using brackets. The difference between the dot notation and the bracket notation is that any expression can be put inside the bracket. See the example below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6o7ajpcphqyytxeg02i6.png)

 
3.	**Destructuring**
Destructuring is a feature in JavaScript that allows you to retrieve data from an object or even an array and store them in variables. This is very useful when we receive data from a web API which is usually stored in objects. The syntax for destructuring an object is quite similar to that used in destructuring an array. The only difference is when destructuring an object, you should use the curly braces like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3k7hkkhc295z7cav9kc.png)

 
In the code above, the curly braces come after the keyword _const_, and the variable names inside the curly braces are identical to the property names. The variables are then used to form a sentence, as seen above.
We can, however, change the variable names like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p91yxw45w6fr0y82jrq6.png)


 
Thus in the code above, the new variable names are defined in a similar way we define properties in object literals.

However, what if the property we want to retrieve does not exist on the object? Normally in such a situation, it is supposed to show us undefined. Nevertheless, when destructuring our object, we can give such variable a default value like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqag6qdu2xx6l1bp6aqk.png)

 
In the above code, the property _birthYear_ does not exist, but we gave it a default value of two thousand and two using the equal to and brackets sign.  

**Object methods and the ‘this’ keyword**
If you have come so far in this article, you will discover that we have used methods a few times. Methods are like properties of an object. They are more dynamic, however. A method is essentially a function that is a property of an object. They can be likened to an action performed on an object. For example, the _calcAge_ method in the code below calculates the age of the object _john_ and returns it. It is then logged to the console in a nice string.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wb3uv2ak6q3rd022ob7.png)

 
The ‘this’ keyword functions differently in JavaScript than it does in other programming languages. It is more like a special variable that is created for every function. The 'this' keyword points to the function in which it is called. This means that it takes the value of the object. The values of the 'this' keyword are, however, not static. It depends on how the function is called, and its value is assigned when the function is executed or called. A function may be called as a property of an object that is, a method, in which case it points to the object that is calling that method. 
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/39talnyk1a4x6qde121x.png)


In the above example, the ‘this’ keyword points to the john object, which is the object calling the _calcAge_ method.
Functions may also be called normally. That is, without it being a property of an object. In this case, the ‘this’ keyword will be undefined if it is in strict mode else, it will point to the global object, which in the case of the browser is the (window object).
An arrow function does not have its own ‘this’ keyword. Using the ‘this’ keyword in an arrow function will only point to its outer or parent function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5crgbcazwpcbjuxwxd63.png)

 
In the example above, the arrow function _func_ points to the outer _calcAge_ method.
Lastly, it is important to note that if a function is called as an event listener, the ‘this’ keyword will always point to the DOM element that the handler function is attached to.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v032i56wfref5xag0lvw.png)


**When to use objects in our code**
Since objects are containers for storing data, usually complex data, it is advisable to use them when we want to do just that. Also, unlike arrays, data in an object does not need to be structured. This means that if we want to store unstructured data, objects are our best bet. We can also decide to organize our code in an object-like manner. This is known as Object Oriented Programming(OOP).

**Conclusion**
This article has tried to give you a brief overview of objects in JavaScript. This includes what objects are, their syntax and how they are created, how we can access object properties, their methods and the 'this' keyword, and when we should use objects in our code. Of course, there is still a lot to learn about objects in JavaScript, such as how prototypes work and Object Oriented Programming in JavaScript. If you wish to learn more, you can visit this site https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects for more info.

This article was written by Okure U. Edet Kingsley. You can follow me on Twitter @itzz_okure