jQuery.isEmptyObject() JQ Home  <<  Utilities  <<  jQuery.isEmptyObject()

Empty object detection.

Description

The jQuery.isEmptyObject() jQuery Type utility method, returns a boolean dependant upon the specified object having properties or not.

Shorthand version $.isEmptyObject()

  • The jQuery.isEmptyObject() checks properties inherited from prototypes, as well as properties of the object specified.
  • For consistent cross-browser results the specified object to test should always be a plain JavaScript object, which can be verified using jQuery.isPlainObject().

Syntax

Signature Description
jQuery.isEmptyObject( object )Return a boolean dependant upon the specified object having properties or not.

Parameters

Parameter Description Type
objectA plain JavaScript object to test for properties.Object

Return

A Boolean object.

jQuery.isEmptyObject( object ) Example Utilities  <<  Top

Return a boolean dependant upon the specified object having properties or not.

In the example below when we press the button we create some variables and test to see if they are plain JavaScript objects. If they are we test to see if they are empty objects and in all cases output a message.


$(function(){
  $('#btn5').one('click', function(){
    var array1 = [obj0 = 'aaa', obj1 = [], obj2 = {}, obj3 = { a: 'b' }, obj4 = new Object];

    for (var i=0; i<array1.length; i++) {
      if ($.isPlainObject(array1[i])) {
        if ($.isEmptyObject(array1[i])) {
           $('#div5').append('obj' + i + ': Plain JavaScript object with no properties <br>');
        } else {
           $('#div5').append('obj' + i + ': Plain JavaScript object with properties <br>');
        }
      } else {
        $('#div5').append('obj' + i + ': is not a Plain JavaScript object <br>');  
      }
    }
  });
});

div5. Some initial text.

Press the button below to action the above code:

Related Tutorials

jQuery Intermediate Tutorials - Lesson 10 - jQuery Copy, Data & Type Utilities