Object in JavaScript

Object is nothing but pair of keys and values (properties and values) specified using JSON syntax. All objects inherit the prototype from Object class.

There are 2 ways of creating objects in JavaScript.

  1. using new Object() constructor
  2. Using  object literals {}

Below image shows how to create an object using Constructor in JavaScript.

creating-object-in-javascript

creating-object-in-javascript

Below image shows alternative syntax to create an Object.

creating-objects-in-javascript

Accessing all properties of an Object
You can access all properties and values of any object using below syntax.

accessing-all-properties-of-an-object

accessing-all-properties-of-an-object

Built-in Objects

Here is the list of some of the famous built in objects.

  1. window
  2. document
  3. screen

You can get all properties of any of these objects using below syntax.

for (property in <object>){console.log(property)}

Here are the examples to get the properties of document and window objects.

for (v in document){console.log(v)}

for (v in window){console.log(v)}

You may also like...