Objects in JavaScript

Everthing in javascript is either primitive data type, like Strings, Numbers, Boolean , undefined, null, or an Object. Arrays, Functions are also Objects, but they are build-in objects of javascript.

JavaScript Objects are basicly 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 using instanceof

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={
    "full-name":"avinash malhotra",
    profile:"UI Developer",
    location:"Noida, Delhi NCR", 
    pincode:201301,    
};
   
    user["full-name"];  // return "avinash malhotra"
    user.profile;         // return "UI Developer"
    user.location;        // return "Noida, Delhi NCR"
    user.pincode;         // return 201301 
      
      or
      
    user["full-name"];     // return "avinash malhotra"  
    user["profile"];  // return "UI Developer"  
      

Check property in object

An Object can have both custom properties and build-in properties (like toString, toLocaleString etc). in operator is used to check whether given property or method (function) exists or not. in operator return value in boolean form, either true or false. See example


    var user={
            "full-name":"avinash malhotra",
            profile:"UI Developer",
            location:"Noida, Delhi NCR", 
            pincode:201301,    
            };
            
    "location" in user;   // returns true   
    "toString" in user;   // returns true   
    "age" in user;        // returns false   
    

hasOwnProperty

The hasOwnProperty property check whether the property is his own property or global property of all JS Objects. It returns boolean value, true or false. in operator returns true for all properties, including global. See example


    var user={
            "full-name":"avinash malhotra",
            profile:"UI Developer",
            location:"Noida, Delhi NCR", 
            pincode:201301,    
            };
            
user.hasOwnProperty("pincode");   // returns true  
user.hasOwnProperty("profile");   // returns true  
user.hasOwnProperty("toString");   // returns false  
    

Add and remove property in objects

An JS Object can add and remove properties in object. Even if object is already declared, still we can add and properties in object

Add Property in Object


    var user={
            "full-name":"avinash malhotra",
            profile:"UI Developer",
            location:"Noida, Delhi NCR", 
            pincode:201301,    
            };
            
    user.country="India";                               
    

Remove Property in Object


    var user={
            "full-name":"avinash malhotra",
            profile:"UI Developer",
            location:"Noida, Delhi NCR", 
            pincode:201301,    
            };
    
    delete user["full-name"];   
    

Change Property value in Object


    var user={
            "full-name":"avinash malhotra",
            profile:"UI Developer",
            location:"Noida, Delhi NCR", 
            pincode:201301,    
            };
    
    user.pincode=110022;
            
    

Get All properties of Object

To get all properties of object, for in loop is required. for in loop can access all properties inside object and array.

full-name - avinash malhotra
profile - UI Developer
location - Noida, Delhi NCR
pincode - 201301

    var user={
            "full-name":"avinash malhotra",
            profile:"UI Developer",
            location:"Noida, Delhi NCR", 
            pincode:201301,    
            };
    for( var i in user){
    console.log(i + " - " + user[i]);
    }