WebMasterCampus
WEB DEVELOPER Resources

jQuery & Javascript Check for an Empty Object

Learn how Javascript & jQuery to Check for an Empty Object


jQuery & Javascript Check for an Empty Object

If you are using a web service that may returns nothing if corresponding data is not available and they sending empty object. So, how can you check using an empty object using Javascript and jQuery.

The following example will use both Javascript and jQuery to give your clear picture how you can achieve this.

Example To Check for an Empty Object using JavaScript

<script>
  //JavaScript Code
    
    function isEmpty(obj){  
      return JSON.stringify(obj) === JSON.stringify({})
    }
    var a = {"Martin": 5};
    var b = {}

    console.log( isEmpty(a) );
    console.log( isEmpty(b) );

</script>

Example To Check for an Empty Object using jQuery

 <script src="https://code.jquery.com/jquery-git.js"></script> 
<script>
  //jQuery Code    
    
    var a = {"Martin": 5};
    var b = {};

    console.log( jQuery.isEmptyObject(a) );
    console.log( jQuery.isEmptyObject(b) );
 </script> 

Read more jQuery isemptyobject

Created with love and passion.