Object in Javascript with example

Object in Javascript with example
  • Object in javascript is a kind of container for storing values or you can say it is a kind of variable with multiple values. As we all know that a javascript variable contains a single value which means we can only assign a single value to a javascript variable.
  • It holds the values using the name: value pair.
  • javascript object is declared with the keyword const.
  • parenthesis is used to declare javascript objects.

Below is an example of a javascript object

Also, you can write the above object as shown below

}

whereas boy is the name of the object
first_name is the property name
Robin is the property value

From the above example, you can see some values inside the object. Those values are known as object properties. These values can be used in the methods if we declare the methods along with the object properties. Also, note that we have to use the first bracket at the time of calling methods using the javascript objects.

If you do not call the method using the first bracket then it will return the function definition declared inside the object instead of returning the result of the defined methods. On the other hand, you can access the properties or methods of an object with the help of an HTML id attribute.

Also read, Google Maps Add Marker Using Maps Javascript API and HTML

Access the properties of an object in javascript

We can access the properties of a javascript object in two ways as given below

first way:- object.property_name that means boy.first_name
second way:- object[“property_name”] that means boy[“property_name”]

Below is an example

Output:-

Object methods:-

Object method is a kind of property inside the javascript object and it is stored in object properties with the help of function definition.

Below is an example

Output:-

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below

Originally published at https://pbphpsolutions.com on September 13, 2022.

--

--

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bipsmedium

Hi, This is Biplab and I am web developer well versed in PHP and other open source technologies. I am here to share my experience with the community.