JavaScript Tutorial – Object

Object is one of the JavaScript data types. Object and variable are very similar except that object can store multiple values.

JavaScript object syntax

   var name = {list of name:value pairs};

The values are written as name:value pairs (name and value separated by a colon). The name:values pairs (in JavaScript objects) are called properties.

Example: how to define object in JavaScript

   var myFriend = {name:"Jay", 
                   age:18,
                   gender:"male"};

Here we defined an object called myFriend and it stores three sets of properties.

How to access object’s properties

   objectName.propertyName

Let’s see an example.

<script type="text/javascript">
   var myFriend = {name:"Jay", 
                   age:18,
                   gender:"male"};

   document.write(myFriend.name);
   document.write('<br>');
   document.write(myFriend.age);
   document.write('<br>');
   document.write(myFriend.gender);
</script>
Try it yourself »

You will learn more about objects later in this tutorial.