Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JavaScript: Arrays are heterogeneous

Arrays in JavaScript are heterogeneous. You can store any kind of data in the array.

var arr = [2, "Hello", 123.5, true, person];
As you see, I can able to store different kinds of data like number, string, boolean in the same array.

array.html






Array



"text/javascript">
var person = {
firstName: "Hari krishna",
}
var arr = [2, "Hello", 123.5, true, person];

document.write("Type of array : " + typeof arr + "
"
);
document.write("arr[0] : " + arr[0] + " type : " + typeof arr[0] + "
"
);
document.write("arr[1] : " + arr[1] + " type : " + typeof arr[1] + "
"
);
document.write("arr[2] : " + arr[2] + " type : " + typeof arr[2] + "
"
);
document.write("arr[3] : " + arr[3] + " type : " + typeof arr[3] + "
"
);
document.write("arr[4] : " + arr[4].firstName + " type : " + typeof arr[4] + "
"
);





Open above page in browser, you can able see following data.

Type of array : object
arr[0] : 2 type : number
arr[1] : Hello type : string
arr[2] : 123.5 type : number
arr[3] : true type : boolean
arr[4] : Hari krishna type : object

Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

JavaScript: Arrays are heterogeneous

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×