What is Object? It’s Types?


In this article, we are learning object type in JavaScript. In the previous article will learn what is primitive type in JavaScript 6 now in this article we will take a closer look at what an object and its type. You will learn what is an object? type of object in JavaScript? and it’s syntax. We will first describe the definition of an object in the simple sentence. Then we create an object with their syntax. We also take a look at what is the property of an object.

There are five types of objects.

  • Array
  • Date
  • Regular Expression
  • Maps and WeakMap
  • Sets and WeakSet

There are five types of object in JavaScript. array date regular expression map and weakmap and then set and weakSet. we will take a brief description of all of them one by one. so let’s start the article with what is an object?

What is Object?

The object is not like a primitive type that immutable and takes only one value at a time. Object Can represent multiple and complex values. It is mutable. So you can change any value of object anytime.

Object type always works as a container. You can put a different kind of data in that container. Object have a literal syntax Open and closing curly braces { }. These curly braces allow us to express an object content.

so let’s create a simple object.

You can put your content inside of this curly braces. The content of an object is called members. You can also call them properties. The member or you can say the properties consist of a key and value pair. The property key is must be a string or a symbol. And the value is can be anything for example primitive types or object type.

So let’s create a property of an object. To create a property you can just specify object not your property name. You can also specify the value to that property or you can leave that as it is so it will take the default value undefined. The Property name must be a valid identifier. in the previous vide,o we learn about what is identifier is all about.

So let’s take an example of an object with properties. To create an object with their properties you need to take care of these things. First object properties are always specified in curly braces. Second, to separate properties you can use a comma ( , ). Third, to separate key and value you can use a colon.

Now let’s take an example of how to create a simple an object.

so we will create a Constant object so we will just say const obj is equal to In the curly braces we will create properties. So the first property names And to specify value we add the colon and specify a string, Vinod. So to create the next property just specify here to separate them and create a new key with the name age and specify age value so we’ll just add colon here and specify 34.

You can create multiple objects with multiple properties so in the above example we created an obj object and in the curly braces be specified two properties with key and value pair.  so we specify a key name and then after the colon, we specified the value of that key is Vinod.

After that, we specify one more property with the key age and value 34. You can also create a function in the object we will talk about this in the future tutorials.

Accessing Object Value

Now to Access the object value we will just specify the object name dot the property name. That’s it now you will get the property value on the console. There is a complex object are there in the JavaScript language but at the beginner level, we just take the basic objects. we will talk about the advanced objects in the advanced JavaScript tutorials.

Now if you wanted to delete the object property then you have a delete keyword that deletes object property from the object. So to delete age property we will just say delete object dot property name age and specify semicolon at the end of the statement.

That’s it now your property is deleted.

If you are from the object oriented programming then this concept will not really hard to understand.

Array

The array is just the collection of data. so the data means here collection of different types. If it is primitive or object type. The array is a sequential data that start with the zero and end with n. so the end refers to the endless value. So let’s take a look the important features of the array. An array element can be any type of data. It may be objects or primitive types.

Array size is not fixed so you can add and remove array element anytime Array is zero-based. So the first element of the array is zero the next element is 1 and so on.

To declare an array we can use Square brackets. And to separate element use a comma (,).

So now if you wanted to check how many elements are there in the array then just specify alanine and call the length method. This method will return the number of elements in your array.

Now let’s talk about how to access this array value.

as I said I didn’t start from zero. So in the first example, The index of the one is zero. The index of 2 is 1 and so on. So to get the value will just write an array name and in the square bracket specify index value. So here I will say zero. Now you will get one on the console. If you specify one in the index you will get to on console.

Now let’s change the array element.

To change the array element specified index value and specify your new value. It will automatically update itself.

Date

So dates represent the date and time using date object. Date object used to get the date or you can use to set date for a particular event and you can also specify time using the date of an object.

To create a date we will write.

Now on the console, you will get the current date. Let me show you how to specify a date to the particular Identifier. And you can also specify time with the date object

So we will just say.

There is the different type of methods of date object where you can get the current date month year time seconds and so on.

Regular Expression

The regular expression is sublanguage of JavaScript and it is used to Perform Complex search and replace operations on strings. The regular expression is a big topic for the explanation it’s beyond the lecture. will cover the regular expression in the separate article.

But just for now keep in mind regular expression is used to perform Complex search and replace operation.

Maps and Sets

In this topic, we will cover map and week map and set and week set. In new ECMAScript 6, a new data type introduced is a map and set And their week counterpart. Weakmap and weakset. Maps are just like an object but it Offers more advantages then object in certain situations. Set are similar to array except they Can’t content duplicate element. Now let’s take a look at how to create a map.

So we will just type.

Using map you can assign a particular role to the user. Using set() method you can specify key and value pair to the map object just like this.

Sets are just a collection of data where duplicate are not allowed. To specify a role to the particular user you can use sets to create the set type.

If you check the set object then you will see user added in that object.

Anyway, this is just the basic of map and sets. If you don’t understand this topic then don’t worry we will cover everything in detail in the future articles.

Now the object type is completed. we had covered 5 type of object of JavaScript. In the subsequent article, we will take a look at how they work with their examples. At the bottom object is just a collection of different kind of data that put together and work as a container.

That is all for this article. we will see you in the next one.