Objects in JavaScript

Everthing in javascript is an Object.

JavaScript Objects are also collection of Multiple datatypes. inside a single variable, same like array, but Arrays use index notation, Objects use key-value pair.

Objects can also store multiple data in a single variable.



How to declare Object in JavaScript

Objects in javascript can be declared using curly brackets {}. We can also use new Object() constructor function to declare Objects. Both {} and new Objects() works same. new Object() is constructor form, and curly brackets {} is literal form of JS Objects.

JavaScript Object Literal

 
    var month={};            // blank Object

JavaScript Object Constructor


    var month=new Object();        // blank Object
        

Check Object datatype

Objects are Reference data types. typeof operator will return "object" for Array and Objects. To check datatype of an object, use instanceof(Object) function. instanceof(Object) function will determine whether the passed value is an Object or not.


var x={};        
typeof x;                // return "object"
x instanceof(Object);    // return true                
        
  1. instanceof of both Arrays and Objects is Object.
  2. For Arrays, use Array.isArray().

Traversal values of Object

To check element inside Object, we can call object.property or object followed by property in brackets. To find first element, call array[0]. This index notation starts from 0 to array.length-1.


var user={
    name:"avinash malhotra",
    profile:"UI Developer",
    location:"Noida, Delhi NCR", 
    pincode:201301,    
};
   
    user.name;        // return "avinash malhotra"
    user.profile;     // return "UI Developer"
    user.location;    // return "Noida, Delhi NCR"
    user.pincode;     // return 201301 
      
      or
      
    user["name"];     // return "avinash malhotra"  
    user["profile"];  // return "UI Developer"