JavaScript Objects

JavaScript objects are written with curly braces. Object properties are written as name:value pairs, separated by commas. This is an example of an object (person) which has 4 properties: firstName, lastName, age, and eyeColor:

  var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

HTML file: Displayed by browser:
<body>
<p id="demo"></p>
<script>
   var person = {
     firstName : "John",
     lastName : "Doe",
     age          : 50,
     eyeColor : "blue",
};
   document.getElementById("demo").innerHTML =
   person.firstName + " is " + person.age + " years old "
   +"with " + person.eyeColor + " eyes.";
</script>
</body>

More about objects later in this tutorial.

BackTable of ContentsNext