Arrays also behave like objects In JavaScript

In JavaScript arrays also behave like objects. An array is collection of variables, an object is also collection of variables. In JavaScript we can access an array subscripts like an object properties by using a dot(.) operator. So in JavaScript we can we have facility to use arrays like object also.
 
Example
 
<html>

<head><title> Arrays are like Objects </title>

</head>

<body bgcolor=green text=”yellow”>

<h4 align=center> We are using array elements as Object properties </h4>

<script language=”JavaScript”>

var demo1= new Array()

demo1[0]=”Car”

demo1[1]=”BUS”

demo1[2]=”Truck”

document.write(“<br> Vehicle Type : “+demo1[0]);

document.write(“<br> Vehicle Type : “+demo1[1]);

document.write(“<br> Vehicle Type : “+demo1[2]);

demo1.name=”Sandeep”

demo1.mname=”Singh”

demo1.lname=”Bhandari”

document.write(“<br> <br>I am :”+demo1.name+” “+demo1.mname+” “+demo1.lname)

document.write(“<br><br> My Name is : “+demo1[“name”]);

document.write(“<br> My Middle Name is : “+demo1[“name”]);

document.write(“<br> My Last Name is : “+demo1[“name”]);

</script>

</body>

</html>

 
Output is:
We are using array elements as Object properties
 
Vehicle Type : Car
Vehicle Type : BUS
Vehicle Type : Truck

I am :Sandeep Singh Bhandari

My Name is : Sandeep
My Middle Name is : Sandeep
My Last Name is : Sandeep

Scroll to Top