Last Updated: February 25, 2016
·
3.69K
· ihcsim

Demystify JQuery isEmptyObject and isPlainObject

Try it out at http://jsfiddle.net/ivan_sim/N8TVV/13/

In short,
$.isPlainObject(object) returns true if object is created by either using {} or new Object. Otherwise, it returns false.

var obj1 = new Object;
console.log($.isPlainObject(obj1)); // true

var obj2 = {};
console.log($.isPlainObject(obj2)); // true

var obj3 = {var1:"val1"};
console.log($.isPlainObject(obj3)); // true

var nullVar = null;
console.log($.isPlainObject(nullVar)); // false

var strVar = "";
console.log($.isPlainObject(strVar)); // false

var array1 = [1,2,3,4];
console.log($.isPlainObject(array1)); // false

$.isEmptyObject(object) returns true if object is a plain Javascript Object and it has properties. If object is not a Javascript Object, results may vary across browser.

var obj1 = new Object;
console.log($.isEmptyObject(obj1)); // true

var obj2 = {};
console.log($.isEmptyObject(obj2)); // true

var obj3 = {var1:"val1"};
console.log($.isEmptyObject(obj3)); // false

Complete example at http://jsfiddle.net/ivan_sim/N8TVV/13/