object in javascript

object in javascript

Object can store one or more values and each values can be in different datatype.

Object is a collection of properties, and each property consists of a key(name) and a value.

Syntax - Create

Syntax - Assign value

Example

Try Yourself

In the above example there are three properties, Object is created in the following format {"Name":"Rajesh","Age":"19","Place":"Mulagumoodu"}.


Array object in javascript

In array object one or more object values are stored within the array.

Example

[{"Name":"Rajesh"},{"Age":"29"}]

In the above example two objects are created and each object contain 1 property are stored in the array.

Example

Try Yourself

In the above example two objects are created and added to the array.

And each object contains two properties and the values are stored as [{"Name":"Name 1","Age":"Age 1"},{"Name":"Name 2","Age":"Age 2"}].

Example explanation

  • Empty array is created - []
  • var obj=new Object() - Empty object is created - {}
  • obj.Name="Name 1" - A property is created - {"Name":"Name 1"}
  • obj.Age="Age 1" - A property is created - {"Age":"Age 1"}
  • ar.push(obj) - Created object is added to the array - [{"Name":"Name 1","Age":"Age 1"}]
  • var obj=new Object() - Empty object is created - {}
  • obj.Name="Name 2" - A property is created - {"Name":"Name 2"}
  • obj.Age="Age 2" - A property is created - {"Age":"Age 2"}
  • ar.push(obj) - Created object is added to the array - [{"Name":"Name 1","Age":"Age 1"},{"Name":"Name 2","Age":"Age 2"}]