JavaScript Objects

JavaScript Objects


JavaScript Objects
 
In this tutorial, you will learn about JavaScript Objects with the help of examples:
In Javascript, An Object is a non- primitive data type. Object is an entity having properties and methods. Everything is an object in javascript.
How to create an object in javascript:
 
Creating Objects in JavaScript 
There are Three ways to creating Objects in JavaScript: -
  • By object literal
  • By Creating instance of object directly (using new keyword)
  • By using an object constructor (using new keyword)
 
JavaScript Object by object literal
 
The Syntax of the JavaScript Object by object literal is as follows: -
 
object={property1:value1,property2:value2.....propertyN:valueN}  
 
Example 1 - 
 
 
Output -
 
Example 2 -
 
 
Output -
 
By Creating instance of object directly
 
var objectname=new Object();
Here, new keyword is used to create an object.
 
Example 1 -
 
 
Output -   
 
 
By using an Object constructor
 
Here, you need to create a function with arguments. Each argument value can be assigned in the current object by using this keyword.
The this Keyword refers to the current object.
 
Example 1 -
 
 
Output -
 
 
 
Example 2 -
 
 
Output -