JavaScript Objects - What are Objects in JavaScript ?
JavaScript Objects
- A javaScript object is an entity having state and behavior (properties and method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.
- JavaScript is an object-based language. Everything is an object in JavaScript.
- JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct create objects.
- We can able to create objects in javascript and also three methods available.They are
- Object literal
- Creating instance of Object directly (using new keyword)
- Using an object constructor (using new keyword)
JavaScript Object by object literal
- In JavaScript, you can create an object using object literals. An object literal is a comma-separated list of name-value pairs wrapped in curly braces. Here's an example of how to create an object using object literal notation.

Sample Code
<html>
<body>
<script>
vehicle={contact:108,name:"ambulance",service:15}
document.write(vehicle.contact+" "+vehicle.name+" "
+vehicle.service);
</script>
</body>
</html>
Output
108 ambulance 15
By creating instance of Object
- In JavaScript, you can also create an object by creating an instance of the Object constructor function. The Object constructor function is built into JavaScript and can be used to create a new object.

Sample Code
<html>
<body>
<script>
var vehicle=new Object();
vehicle.contact=104;
vehicle.name="Fire engine";
vehicle.service=8;
document.write(vehicle.contact+" "+vehicle.name+" "+vehicle.service);
</script>
</body>
</html>
Output
104 Fire engine 8
By using an Object constructor
- Here, you need to create function with arguments. Each argument value can be assigned in the current object by using this keyword.

Sample Code
<html>
<body>
<script>
function emp(id,name,salary){
this.id=id;
this.name=name;
this.salary=salary; }
e=new emp(001,"Venkat",3000000);
document.write(e.id+" "+e.name+" "+e.salary);
</script>
</body>
</html>
Output
1 Venkat 3000000