jQuery API

jQuery.isPlainObject()

jQuery.isPlainObject( object ) Returns: Boolean

Description: Check to see if an object is a plain object (created using "{}" or "new Object").

  • version added: 1.4jQuery.isPlainObject( object )

    objectThe object that will be checked to see if it's a plain object.

Note: Host objects (or objects used by browser host environments to complete the execution environment of ECMAScript) have a number of inconsistencies which are difficult to robustly feature detect cross-platform. As a result of this, $.isPlainObject() may evaluate inconsistently across browsers in certain instances.

An example of this is a test against document.location using $.isPlainObject() as follows:

console.log($.isPlainObject(document.location));

which throws an invalid pointer exception in IE8. With this in mind, it's important to be aware of any of the gotchas involved in using $.isPlainObject() against older browsers. Some basic example of use-cases that do function correctly cross-browser can be found below.

Example:

Check an object to see if it's a plain object.

jQuery.isPlainObject({}) // true
jQuery.isPlainObject("test") // false

Support and Contributions

Need help with jQuery.isPlainObject() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.isPlainObject()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://bender.fesb.hr/~robert/ Robert Katić

    Replace 2 “isObjectLiteral” with “isPlainObject”.

  • http://ejohn.org/ John Resig

    Good catch, fixed.

  • Anonymous

    “The object that will be checked to see if it’s an object literal.”
    - if it’s a plain object.

  • http://twitter.com/renosa Reno S. Anwari

    Not yet. One more to go :)

  • LorenW

    Why is a function like this important? A short use case would be helpful. I don’t want to miss out on the usefulness of any of the new functions!

  • http://www.learningjquery.com/ Karl Swedberg

    fixed.

  • fbuchinger

    It's pretty hard to detect a plain object in javascript, mostly due to the “bad parts” of javascript.

    the most obvious solution would be
    if (typeof(myvar) === “object”)

    BUT:
    typeof(null) also returns “object”

    so jQuery.isPlainObject() can be used to reliably check whether a variable is really an object and not null.

    A use case would be to check the data type of a variable stored with $.data().

    Furthermore many Javascript programmers initialise their variables with null and assign them values later depending on certain events or conditions. This is the source of the common 'var xy is null or not an object' error: The programmer assumed that xy is currently an object, but it still is null because no condition was met.

  • fbuchinger

    It's pretty hard to detect a plain object in javascript, mostly due to the “bad parts” of javascript.

    the most obvious solution would be
    if (typeof(myvar) === “object”)

    BUT:
    typeof(null) also returns “object”

    so jQuery.isPlainObject() can be used to reliably check whether a variable is really an object and not null.

    A use case would be to check the data type of a variable stored with $.data().

    Furthermore many Javascript programmers initialise their variables with null and assign them values later depending on certain events or conditions. This is the source of the common 'var xy is null or not an object' error: The programmer assumed that xy is currently an object, but it still is null because no condition was met.