JavaScript Tutorials - Herong's Tutorial Examples - 2.33, by Herong Yang
Differences between "Object" and "Array"
This section provides a quick description of similarities and differences of 'Array' and 'Object'. A tutorial example is provided on how to compare an 'Array' object to an 'Object' object.
JavaScript supports a number of predefined core object types. Among them, "Object" and "Array" have very close relations. If you compare "Array" with "Object", you should see the following similarities and differences:
Here is a JavaScript tutorial example that shows how "Array" and "Object" are similar to each other:
<html> <!-- Object_and_Array.html Copyright (c) 2008 HerongYang.com. All Rights Reserved. --> <head> <title>Comparing Object and Array</title> </head> <body> <pre> <script type="text/javascript"> // Creating an object of "Array" - array literal var bookArray = [ "JavaScript Tutorial", "Herong Yang", 2008, "Programming" ]; // Creating an object of "Object" - object literal var bookObject = { title: "JavaScript Tutorial", author: "Herong Yang", year: 2008, category: "Programming" }; // Checking "bookArray" document.writeln(); document.writeln("About the \"Array\" object:"); document.writeln(" Type: "+(typeof bookArray)); document.writeln(" Instance Of Object: " +(bookArray instanceof Object)); document.writeln(" Instance Of Array: " +(bookArray instanceof Array)); document.writeln(" Members:"); for (var item in bookArray) { document.writeln(" "+item+": "+bookArray[item]); } // Checking "bookObject" document.writeln(); document.writeln("About the \"Object\" object:"); document.writeln(" Type: "+(typeof bookObject)); document.writeln(" Instance Of Object: " +(bookObject instanceof Object)); document.writeln(" Instance Of Array: " +(bookObject instanceof Array)); document.writeln(" Members:"); for (var item in bookObject) { document.writeln(" "+item+": "+bookObject[item]); } </script> </pre> </body> </html>
The output of this tutorial example shows that how an "Array" object is similar to an "Object" object:
About the "Array" object: Type: object Instance Of Object: true Instance Of Array: true Members: 0: JavaScript Tutorial 1: Herong Yang 2: 2008 3: Programming About the "Object" object: Type: object Instance Of Object: true Instance Of Array: false Members: title: JavaScript Tutorial author: Herong Yang year: 2008 category: Programming
Table of Contents
ECMAScript Language Specification and JavaScript Dialects
Data Types, Variables and Expressions
Creating, Accessing, and Manipulating Arrays
Defining and Calling Functions
Web Browser Supporting JavaScript
Server-Side and Client-Side Web Scripting
Adding and Deleting Object Own Properties
Adding and Deleting Object Own Methods
Using "this" Keyword to Represent Current Object
Object Literals of the "Object" Type
Objects with Indexed Properties
►Differences between "Object" and "Array"
Using "Array" Objects as "Object" Objects
Defining Your Own Object Types
Inheritance of Properties and Methods through the Prototype Object Chain
'jrunscript' - JavaScript Shell Command from JDK
Using Functions as "Function" Objects
Introduction to Built-in Object Types
W3C's Document Object Model (DOM) Specifications