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 name: value pair.
  • javascript object is declared with the keyword const.
  • parenthesis is used to declare javascript objects.

Syntax:-
const object = {name:value,name1:value1,name2:value2};

Below is an example of a javascript object

const boy = {first_name: "Robin",last_name: "Sinha",age:30};

Also, you can write the above object as shown below
const boy = {
first_name: "Robin",
last_name: "Sinha",
age: 30
};

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

<html>
<head>
<title>javascript example</title>
</head>
<body>
	<p>Below paragraph includes the name and age of the boy</p>
	<p id="obj" style="font-weight: bolder;"></p>
<script>
	const boy = {
		first_name: "Robin",
		last_name: "Sinha",
		age: 23,
	};

	document.getElementById("obj").innerHTML = "The name of the boy is "+boy.first_name+" and age is "+boy.age;
</script>
</body>
</html>

Output:-

Below paragraph includes the name and age of the boy

The name of the boy is Robin and age is 23

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

<html>
<head>
<title>Javascript example</title>
</head>
<body>
	 <p>Full name of the boy is</p>
	<p id="fullname"></p>
<script>
	const boy = {
		first_name: "Robin",
		last_name: "Sinha",
		age: 23,
		 full_name: function(){
		 	return this.first_name+" "+this.last_name;
		 }
		
	};
	document.getElementById("fullname").innerHTML = boy.full_name();
</script>
</body>
</html>

Output:-

Full name of the boy is

Robin Sinha

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

Leave a Comment

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311