Fiat

Real Life Objects, Properties, and Methods

In real life, a car is an object. A car has properties like weight and color, and methods like start and stop. All cars have the same properties, but the property values differ from car to car. All cars have the same methods, but the methods are performed at different times:

JavaScript Objects

We know that JavaScript variables are containers for data values. This code assigns a simple value (Fiat) to a variable named car:
var car = "Fiat";

Objects are variables too. But objects can contain many values. In this example, the code assigns many values (Fiat, 500, white) to a variable named car:

HTML file: Displayed by browser:
<body>
<p>Creating a JavaScript Object.</p>
<p id="demo"></p>
<script>
    var car = {type:"Fiat", model:500, color:"white"};
   document.getElementById("demo").innerHTML = car.type;
</script>
</body>

Creating a JavaScript Object.

The values are written as name:value pairs (name and value separated by a colon). JavaScript objects are containers for named values.

BackTable of ContentsNext